Byte Conversion Problem

G

Guest

If you take this Byte Arra
Dim bytes() As Byte = {
207, 224, 135, 161, 253, 233, 111, 110, 99, 111, 100, 105, 110, 103,
32, 69, 120, 97, 109, 112, 108, 101

and write the byte array to disk
FileOpen(2, "f:\aaapicture_album\result.txt", OpenMode.Binary
FilePut(2, bytes
FileClose(2
then convert it into a string --->

'Convert the byte array back into a string
strEncryptedFile = textConverter.GetString(bytes

and write it to disk
FileOpen(3, "f:\aaapicture_album\result2.txt", OpenMode.Binary
FilePut(3, strEncryptedFile
FileClose(3

The result messes up the first 8 bytes. The hex equivalants got changed
What am I missing??
Richar
 
J

Justin Weinberg

You're getting this behavior because of the default behavior of FilePut

From the docs:

If the variable being written is an object containing a string, FilePut
writes a two byte descriptor identifying the VarType(8) of the object, a
two-byte descriptor indicating the length of the string, and then writes the
string data. The record length specified by the RecordLength parameter in
the FileOpen function must be at least four bytes greater than the actual
length of the string. If you wish to put a string without the descriptor,
then you should pass True to the StringIsFixedLength parameter, and the
string you read into should be the correct length.


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmPut.asp

You may want to look at FileStreams instead.
 
G

Guest

Justin
If the bytes are changed to this:
Dim bytes() As Byte = { _
65, 83, 67, 73, 73, 32, 69, _
110, 99, 111, 100, 105, 110, 103, _
32, 69, 120, 97, 109, 112, 108, 101}

write the byte array to disk:
FileOpen(2, "f:\aaapicture_album\result.txt", OpenMode.Binary)
FilePut(2, bytes)
FileClose(2)
then convert it into a string --->

'Convert the byte array back into a string.
strEncryptedFile = textConverter.GetString(bytes)

and write it to disk:
FileOpen(3, "f:\aaapicture_album\result2.txt", OpenMode.Binary)
FilePut(3, strEncryptedFile)
FileClose(3)


Both result.txt and result2.txt compare exactly.

Richard
 
J

Jay B. Harlow [MVP - Outlook]

Richard,
Remember that strings in VB.NET are 16 bit Unicode characters which means
each character is 16 bits. That Unicode is an encoding, there are lots of
different character encodings, some take 16 bits, some take 8 bits, some
take 32 bits, some take 64. It appears that you have some OTHER encoding
going on in your Byte array, unless you inform the file stream which
encoding to use, it will use a default encoding, which I suspect does not
match the encoding in your byte array, hence the difference. The easiest way
to control which encoding is used is to use the classes in System.IO, mainly
FileStream & StreamReader & StreamWriter.

When you create the StreamReader be certain that you use the correct
Encoding class for your file.

http://msdn.microsoft.com/library/d.../frlrfSystemIOStreamReaderClassctorTopic4.asp

http://msdn.microsoft.com/library/d.../frlrfSystemIOStreamReaderClassctorTopic6.asp

For information on Encoding, Unicode & character sets see:
http://www.joelonsoftware.com/articles/Unicode.html
&
http://www.yoda.arachsys.com/csharp/unicode.html

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top