By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,535 Members | 881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,535 IT Pros & Developers. It's quick & easy.

Byte Conversion Problem

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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/de...l/vastmPut.asp

You may want to look at FileStreams instead.

--

Justin Weinberg
Designing a PrintDocument? Drawing to forms?
Check out GDI+ Architect at www.mrgsoft.com
"Richard506" <an*******@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
If you take this Byte Array
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???
Richard

Nov 20 '05 #2

P: n/a
Then after the first two bytes, they should be the same, no???
They are not
Richard
Nov 20 '05 #3

P: n/a
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
Nov 20 '05 #4

P: n/a
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/de...ctorTopic4.asp

http://msdn.microsoft.com/library/de...ctorTopic6.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

"Richard506" <an*******@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
If you take this Byte Array
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???
Richard

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.