Binary Writer Problem

Discussion in 'Programming & Scripting' started by mojobojo, Jan 27, 2009 with 11 replies and 418 views.

  1. mojobojo

    mojobojo Enthusiast

    Messages:
    694
    Ratings:
    96
    Every time I use binary writer to write to a file Text has one extra byte (an unreadable character) added to the begenning of the text. When I use it to write hex to a file it writes four bytes to it instead of the one byte I specified. Here is what I used.

    Code:
    [SIZE=2]bw.Write(textBox1.Text);[/SIZE]
    
    Code:
    [SIZE=2]bw.BaseStream.Position = 3;[/SIZE]
    [SIZE=2]bw.Write(0x6F);[/SIZE]
    
    Any suggestions on this?

     
  2. Haxalot88

    Haxalot88 VIP VIP

    Messages:
    8,051
    Ratings:
    2,482
    what character is added to the beginning of the file? if it's a null byte (0x00), then check your file pointer. For the second example, since the file length isn't even 4 bytes long, it has to add null bytes to the beginning of the file (so it will write 00 00 00 6F since pointers start from 0 not 1)
     
  3. OP
    mojobojo

    mojobojo Enthusiast

    Messages:
    694
    Ratings:
    96
    It adds 0x0C at the beginning of the text. Im about to try what you told me.
     
  4. dschu012

    dschu012 Member

    Messages:
    760
    Ratings:
    381
    What was the actual text in the textbox when it added 0x0C to the beginning? You did bw.write(textBox1.Text); right?
     
  5. OP
    mojobojo

    mojobojo Enthusiast

    Messages:
    694
    Ratings:
    96
    I used bw.write("<Grn><Color>") thats what added 0x0C at the begininng. I dont know if other text changes it. But with bw.write(TextBox1.text) it dose the same thing.All I know it is not null.

    [​IMG]
     
  6. dschu012

    dschu012 Member

    Messages:
    760
    Ratings:
    381
    When writing ASCII string is will write the length of the string before the actual string. For instance the first thing you wrote was 12 characters long so it wrote 0x0c which is 12. When you wrote the second thing "<06>" the string has a length of 4 characters so it wrote 0x04 before it. Null is 0x00 just because there isn't a character representation for the byte doesn't mean it is null.
     
  7. GoldBl4d3

    GoldBl4d3 Retired Retired

    Messages:
    3,758
    Ratings:
    1,328
    um, idk but if its an int32 or somthing. Then int32 and floats/singles are 4 bytes so. But you may be thinking of somthing else
     
  8. Haxalot88

    Haxalot88 VIP VIP

    Messages:
    8,051
    Ratings:
    2,482
    convert the string to a byte array, and then write the bytes instead.

    So within your function, you can do this:
    Code:
    System.Text.ASCIIEncoding  encoding = new System.Text.ASCIIEncoding();
    byte[] bytes_from_string = encoding.GetBytes(TextBox1.text);
    bw.Write(bytes_from_string);
    
    I currently don't have VS, so I couldn't test it, but it SHOULD work.
     
  9. dschu012

    dschu012 Member

    Messages:
    760
    Ratings:
    381
    What Haxalot said should fix your problem or condensed down to one line
    Code:
    bw.Write(System.Text.Encoding.ASCII.GetBytes(textBox1.Text));
    
     
  10. OP
    mojobojo

    mojobojo Enthusiast

    Messages:
    694
    Ratings:
    96

    Thank You That Worked!
     
  11. dschu012

    dschu012 Member

    Messages:
    760
    Ratings:
    381
    Thank Haxalot I just condensed his code to 1 line.
     
  12. OP
    mojobojo

    mojobojo Enthusiast

    Messages:
    694
    Ratings:
    96
    Sorry I ment to thank both of you at once. Thank you Haxalot and dschu012!