469,315 Members | 1,510 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

convert int[] to byte[]

Is there a way to convert int[] to byte[] easily?

I want to write my int[] indexTbl to a MemoryStream but the MemoryStream
class only accepts byte[] buffer. I have more than one int[] indexTbl to
write to the stream so it would be nice if I could just write the whole int[]
as a block and then read it back later when needed.

I thought the Buffer class might be handy here but it seems to be best for
converting byte[] to int[] with Block Copy.

Any suggestions here. I am new to C# and wanted to avoid a for-loop and
convert each int to a byte and then write to the stream one by one.
Nov 17 '05 #1
6 25315
Hi,
One thing you can do is convert each int to bytes and write them in the
stream, youdo so using BitConverter.GetBytes
foreach( int i in the_int_Array)
memoryStream1.Write( BitConverter.GetBytes( i);

I really don't remember a better way.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Mimi" <Mi**@discussions.microsoft.com> wrote in message
news:90**********************************@microsof t.com...
Is there a way to convert int[] to byte[] easily?

I want to write my int[] indexTbl to a MemoryStream but the MemoryStream
class only accepts byte[] buffer. I have more than one int[] indexTbl to
write to the stream so it would be nice if I could just write the whole
int[]
as a block and then read it back later when needed.

I thought the Buffer class might be handy here but it seems to be best for
converting byte[] to int[] with Block Copy.

Any suggestions here. I am new to C# and wanted to avoid a for-loop and
convert each int to a byte and then write to the stream one by one.

Nov 17 '05 #2
"Mimi" <Mi**@discussions.microsoft.com> wrote in message
news:90**********************************@microsof t.com...
Is there a way to convert int[] to byte[] easily?

I want to write my int[] indexTbl to a MemoryStream but the MemoryStream
class only accepts byte[] buffer. I have more than one int[] indexTbl to
write to the stream so it would be nice if I could just write the whole int[] as a block and then read it back later when needed.

I thought the Buffer class might be handy here but it seems to be best for
converting byte[] to int[] with Block Copy.

Any suggestions here. I am new to C# and wanted to avoid a for-loop and
convert each int to a byte and then write to the stream one by one.


I would do it this way

int[] ints = new int[3];

ArrayList arrList = new ArrayList();

arrList.AddRange(ints);

byte[] bytes = (byte[]) arrList.ToArray(typeof(byte));

the ArrayList has a handy ToArray method.

regards Richard
Nov 17 '05 #3
<"Richard Kure" <None>> wrote:
I would do it this way

int[] ints = new int[3];

ArrayList arrList = new ArrayList();

arrList.AddRange(ints);
byte[] bytes = (byte[]) arrList.ToArray(typeof(byte));
the ArrayList has a handy ToArray method.


Yes, but it doesn't convert things from one type to another. The above
code gives an InvalidCastException.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
Mimi <Mi**@discussions.microsoft.com> wrote:
Is there a way to convert int[] to byte[] easily?

I want to write my int[] indexTbl to a MemoryStream but the MemoryStream
class only accepts byte[] buffer. I have more than one int[] indexTbl to
write to the stream so it would be nice if I could just write the whole int[]
as a block and then read it back later when needed.

I thought the Buffer class might be handy here but it seems to be best for
converting byte[] to int[] with Block Copy.

Any suggestions here. I am new to C# and wanted to avoid a for-loop and
convert each int to a byte and then write to the stream one by one.


I'd suggest you use a BinaryWriter, assuming you want to convert each
int to four bytes. If you need to convert each int to *one* byte,
you'll need to loop through.

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

How the use of a BinaryWriter will avoid the need of a loop?
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Mimi <Mi**@discussions.microsoft.com> wrote:
Is there a way to convert int[] to byte[] easily?

I want to write my int[] indexTbl to a MemoryStream but the MemoryStream
class only accepts byte[] buffer. I have more than one int[] indexTbl to
write to the stream so it would be nice if I could just write the whole
int[]
as a block and then read it back later when needed.

I thought the Buffer class might be handy here but it seems to be best
for
converting byte[] to int[] with Block Copy.

Any suggestions here. I am new to C# and wanted to avoid a for-loop and
convert each int to a byte and then write to the stream one by one.


I'd suggest you use a BinaryWriter, assuming you want to convert each
int to four bytes. If you need to convert each int to *one* byte,
you'll need to loop through.

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

Nov 17 '05 #6
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us>> wrote:
How the use of a BinaryWriter will avoid the need of a loop?


You're right, it doesn't. However, you don't need to do anything
particular with each int to convert it to bytes - you can just write
the int directly.

I don't believe that there's any real benefit in avoiding looping here.

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Bryan Parkoff | last post: by
6 posts views Thread by Allan Ebdrup | last post: by
5 posts views Thread by bbb | last post: by
19 posts views Thread by tshad | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.