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

Conversion of byte[]

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Tom,

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

Pieter

"Tom Bean" <tb***@t-a-c.com> wrote in message
news:eC****************@TK2MSFTNGP12.phx.gbl...
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

Nov 13 '05 #2

P: n/a
Pieter <Pi****@N.O.S.P.4.M> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #3

P: n/a
Oooops! I didn't thought about that, just tried to help.

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Pieter <Pi****@N.O.S.P.4.M> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 13 '05 #4

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

"Tom Bean" <tb***@t-a-c.com> wrote in message
news:eC****************@TK2MSFTNGP12.phx.gbl...
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

Nov 13 '05 #5

P: n/a
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" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Tom Bean <tb***@t-a-c.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 13 '05 #6

P: n/a
Tom Bean <tb***@t-a-c.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #7

P: n/a
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" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Tom Bean <tb***@t-a-c.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 13 '05 #8

P: n/a
Tom Bean <tb***@t-a-c.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.