Why am I getting this?

  • Thread starter Thread starter Scott M.
  • Start date Start date
S

Scott M.

Why does the following code produce a string ("Byte me!") in the output
file, rather than byte data as integers (66 121 116 101 32 109 101 33)?

Dim fs As System.IO.FileStream = System.IO.File.Open("C:\myMessage.dat",
IO.FileMode.Create)
Dim msg As String = "Byte me!"
Dim msgByteArray() As Byte = System.Text.Encoding.Default.GetBytes(msg)
fs.Write(msgByteArray, 0, msgByteArray.Length)

....And, how do I get the output file to have the byte array data?

PS - I know I could use a different Stream type, but I want to do this using
a FileStream.
 
Scott M. said:
Why does the following code produce a string ("Byte me!") in the output
file, rather than byte data as integers (66 121 116 101 32 109 101 33)?

Dim fs As System.IO.FileStream = System.IO.File.Open("C:\myMessage.dat",
IO.FileMode.Create)
Dim msg As String = "Byte me!"
Dim msgByteArray() As Byte = System.Text.Encoding.Default.GetBytes(msg)
fs.Write(msgByteArray, 0, msgByteArray.Length)

...And, how do I get the output file to have the byte array data?

PS - I know I could use a different Stream type, but I want to do this using
a FileStream.

It does show those values. I'm guessing you are using a text editor rather
than something like Notepad++ that can show the values as bytes. The bytes
you want and the string you got are the same.
 
As Mike says, you are actually getting the result you want.

However, you could try a BinaryWriter if you want to write other data types
(e.g. Integer) in binary format.
 
Scott,

I am not sure, however for one thing I am sure, your default text encoding
is not ASCII while you are showing us here a decimal representation of bit
values in 7 bit (bit)words confirm ASCII.

Cor
 
Hello Scott,

Apart from Mike, Davud and Cor's suggestions, if you expect to see the
integers (66 121 ...) in the output file, we can do it in this way:

Dim fs As System.IO.FileStream =
System.IO.File.Open("C:\myMessage.dat", IO.FileMode.Create)
Dim msg As String = "Byte me!"
Dim msgByteArray() As Byte =
System.Text.Encoding.Default.GetBytes(msg)
For Each bt As Byte In msgByteArray
Dim tmpByteArray() As Byte =
System.Text.Encoding.Default.GetBytes(bt.ToString())
fs.Write(tmpByteArray, 0, tmpByteArray.Length)
Next
fs.Close()

Hope it helps,

If you have any other concerns or questions, please feel free to let me
know.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Scott

Would you mind letting me know the result of the suggestions? If you need
further assistance, feel free to let me know. I will be more than happy to
be of assistance.

Have a great day!

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top