convert int[] to byte[]

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
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,
 
Mimi said:
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
 
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.
 
Mimi said:
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.
 
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 said:
Mimi said:
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.
 
<"Ignacio Machin \( .NET/ C# MVP \)" <ignacio.machin AT
dot.state.fl.us> said:
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.
 
Back
Top