Conversion of byte[]

T

Tom Bean

I have several data files containing ASCII strings, int, long and byte data
types and need to store the data in variables of the appropriate type. From
previous posts, there seem to be several methods for converting byte[] to
strings.

What is the best way to convert a byte[] to a long or int?

Thanks,
Tom
 
P

Pieter

Tom,

I don't see the point in converting an array (multiple values) to a basic
data type (1 value).

Pieter
 
J

Jon Skeet

Pieter said:
I don't see the point in converting an array (multiple values) to a basic
data type (1 value).

As Tom posted, the original file *contains* int, long and byte data.
Clearly it has to be represented in bytes, as that's the only way that
anything can be represented in files, fundamentally.

BitConverter is almost certainly the way to go here.
 
T

Tom Bean

I checked the documentation on System.BitConverter.ToString and unless I'm
misreading it, it appears to convert the byte[] to a hex-string instead of a
normal string of characters. If that is correct, it isn't what I need. Is
there a preferred method for converting byte[] to string?

Also, once I have converted the byte[] to the variables it contains, I need
to convert them back to a byte[] for updating the file.
System.BitConverter.GetBytes seems to be able to convert from most numeric
data types to byte[]. What is the best method to convert string back to
ASCII in the byte[]?

Thanks,
Tom
 
T

Tom Bean

Jon,

Thanks for your help.

Another question: My byte[] from the file contains arrays of bytes, shorts,
ints, etc. The files were created in C++ where it is a simple matter to use
pointers to move through the file's array of bytes and copy/convert the
data.

What is the best method of copying these embedded arrays into other arrays?

Thanks,
Tom

Jon Skeet said:
Tom Bean said:
I checked the documentation on System.BitConverter.ToString and unless I'm
misreading it, it appears to convert the byte[] to a hex-string instead of a
normal string of characters. If that is correct, it isn't what I need. Is
there a preferred method for converting byte[] to string?

Yes. Use System.Text.Encoding.
See http://www.pobox.com/~skeet/csharp/unicode.html for more details.
Also, once I have converted the byte[] to the variables it contains, I need
to convert them back to a byte[] for updating the file.
System.BitConverter.GetBytes seems to be able to convert from most numeric
data types to byte[]. What is the best method to convert string back to
ASCII in the byte[]?

Again, System.Text.Encoding.
 
J

Jon Skeet

Tom Bean said:
Another question: My byte[] from the file contains arrays of bytes, shorts,
ints, etc. The files were created in C++ where it is a simple matter to use
pointers to move through the file's array of bytes and copy/convert the
data.

What is the best method of copying these embedded arrays into other arrays?

You generally don't need to - just keep an index of where you are, and
give the index to BitConverter.

Alternatively, often BinaryReader will do what you want.

If you need to copy arrays around, just create a new array of the right
size and then use Array.Copy.
 
T

Tom Bean

Jon,

I encountered a problem trying to copy the byte[] to a short[]. Array.Copy
expects the source and destination to be the same size. Is there any
alternative besides using BitConverter to convert each pair of bytes to a
short?

Thanks,
Tom

Jon Skeet said:
Tom Bean said:
Another question: My byte[] from the file contains arrays of bytes, shorts,
ints, etc. The files were created in C++ where it is a simple matter to use
pointers to move through the file's array of bytes and copy/convert the
data.

What is the best method of copying these embedded arrays into other
arrays?

You generally don't need to - just keep an index of where you are, and
give the index to BitConverter.

Alternatively, often BinaryReader will do what you want.

If you need to copy arrays around, just create a new array of the right
size and then use Array.Copy.
 
J

Jon Skeet

Tom Bean said:
I encountered a problem trying to copy the byte[] to a short[]. Array.Copy
expects the source and destination to be the same size. Is there any
alternative besides using BitConverter to convert each pair of bytes to a
short?

You can use Buffer.BlockCopy - hopefully that should do what you want.
 

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