Convert int/short/long to bytes[]

  • Thread starter Thread starter babylon
  • Start date Start date
B

babylon

I can't find any functions in system.convert to convert
a 4-byte integer to 4 bytes array...
pls help
 
Hi,
Here is an example for int to byte[4] conversion:

buf[0] = (byte) value;
buf[1] = (byte) value >> 8;
buf[2] = (byte) value >> 16;
buf[3] = (byte) value >> 24;

--
Andrew Gnenny
pulsar2003@/no-spam/email.ru (Please remove /no-spam/ for reply)
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE
 
babylon said:
I can't find any functions in system.convert to convert
a 4-byte integer to 4 bytes array...
pls help

There isn't one in System.Convert, however, System.BitConverter.GetBytes()
will do what your need.
 
thx!

any builtin-library for converting an int from little endian to big endian?
as I want to send my data over the network....
i can only find "System.Text.ASCIIEncoding.BigEndianUnicode", which is not
what i wanted...

thx!
 
got it
NetworkToHostOrder of IPAddress


babylon said:
thx!

any builtin-library for converting an int from little endian to big endian?
as I want to send my data over the network....
i can only find "System.Text.ASCIIEncoding.BigEndianUnicode", which is not
what i wanted...

thx!
 
I can't find any functions in system.convert to convert
a 4-byte integer to 4 bytes array...


I have been using System.BitConvert.GetBytes() in my code at work.
Something like this works for me:

byte[] bytes = System.BitConverter.GetBytes(MyIntegerValue);
 
Back
Top