[simple?] convert an unsigned long to byte array and vice versa

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

Guest

Is it possible to convert an unsigned long to byte array. For instance, unsigned long = 23480923

unsigned long longNumber;
longNumber=23480923; // in binary = 1 0110 0110 0100 1010 0101 1011
// so i want byteArray[0]=0000 0001 , byteArray[1]=0110 0110 ,
// byteArray[2]=0100 1010, byteArray[3]=0101 1011.

Is there a way to get the 4 bytes that make up this variable?
Also, is there a way to take a 4 byte array and convert it into a long?

TIA,
War Eagle
 
War Eagle said:
Is it possible to convert an unsigned long to byte array.

You can use BitConverter.GetBytes(ulong). However, that will use the
endianness of the platform, which may not be what you want.

Like BinaryWriter/BinaryReader, you can't specify the endianness. I
keep meaning to do something about that with utility classes...

(Use BitConverter.ToUInt64 for the reverse, btw.)
 

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

Back
Top