473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Byte Conversion Problem

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
4 1987
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
Then after the first two bytes, they should be the same, no???
They are not
Richard
Nov 20 '05 #3
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T"...
43
by: Bill Cunningham | last post by:
I've been reading the C standard online and I'm puzzled as to what multibyte chars are. Wide chars I believe would be characters for languages such as cantonese or Japanese. I know the ASCII...
1
by: Wasim Akram | last post by:
Hi, I have a field "Month" in my SQL server table. The type of this field is "tinyint". Now what I am doing in the code is using DataRow to read this field in a 'int' variable. int month...
4
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
5
by: John J. Hughes II | last post by:
I need to convert a byte array to a structure with conversion on the byte order. I found the below deserilizer which works for getting the data converted to a structure but it's in the wrong byte...
3
by: pkumar | last post by:
How to convert this byte array to string byte b=new byte; Is there any function or I need read one by one and build the string thanks
5
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid (pulled from sql server table with a data type of...
6
by: Nils Wolf | last post by:
hi, how do i convert an integer array to a byte array??
5
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.