What's new

Binary Writer Problem

M

mojobojo

Enthusiast
Messages
684
Reaction score
96
Points
95
Sin$
7
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?
 
Haxalot88

Haxalot88

VIP
VIP
Messages
7,970
Reaction score
2,668
Points
590
Sin$
0
mojobojo said:
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?
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)
 
M

mojobojo

Enthusiast
Messages
684
Reaction score
96
Points
95
Sin$
7
It adds 0x0C at the beginning of the text. Im about to try what you told me.
 
D

dschu012

Enthusiast
Frame In Gold Seasoned Veteran
Messages
757
Reaction score
380
Points
125
Sin$
0
mojobojo said:
It adds 0x0C at the beginning of the text. Im about to try what you told me.
What was the actual text in the textbox when it added 0x0C to the beginning? You did bw.write(textBox1.Text); right?
 
M

mojobojo

Enthusiast
Messages
684
Reaction score
96
Points
95
Sin$
7
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.

hello.jpg
 
D

dschu012

Enthusiast
Frame In Gold Seasoned Veteran
Messages
757
Reaction score
380
Points
125
Sin$
0
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.
 
GoldBl4d3

GoldBl4d3

VIP
VIP
Retired
Programmer Mythical Veteran Legendary Veteran
Messages
3,785
Reaction score
1,452
Points
600
Sin$
0
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
 
Haxalot88

Haxalot88

VIP
VIP
Messages
7,970
Reaction score
2,668
Points
590
Sin$
0
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.
 
D

dschu012

Enthusiast
Frame In Gold Seasoned Veteran
Messages
757
Reaction score
380
Points
125
Sin$
0
What Haxalot said should fix your problem or condensed down to one line
Code:
bw.Write(System.Text.Encoding.ASCII.GetBytes(textBox1.Text));
 
M

mojobojo

Enthusiast
Messages
684
Reaction score
96
Points
95
Sin$
7
dschu012 said:
What Haxalot said should fix your problem or condensed down to one line
Code:
bw.Write(System.Text.Encoding.ASCII.GetBytes(textBox1.Text));


Thank You That Worked!
 
M

mojobojo

Enthusiast
Messages
684
Reaction score
96
Points
95
Sin$
7
dschu012 said:
Thank Haxalot I just condensed his code to 1 line.

Sorry I ment to thank both of you at once. Thank you Haxalot and dschu012!
 
Top Bottom
Login
Register