locked
Int to Byte Array RRS feed

  • Question

  • Hey guys,
    Int to Byte Array, please!?!

    The byte array needs to be a fixed size of 4.

    array<Byte> ^byte = gcnew array<Byte>(4);

    and should be padded with 0x00's

    So if the incoming int = 18, I want the byte array to look like
    { 0x00, 0x00, 0x00, 0x12 }

    Thanks!

    Monday, November 6, 2006 1:22 PM

Answers

All replies

  • See BitConverter.GetBytes Method (Int32):

    http://msdn2.microsoft.com/en-us/library/de8fssa4(VS.80).aspx

    Monday, November 6, 2006 1:34 PM
  • Alex,
    That looks like what I need.  Can the BitConverter be forced to put the parameter-to-be-converted into the LSB position rather than MSB???
    For examp, when I convert an int = 18, i get

    { 0x12, 0x00, 0x00, 0x00 }

    How can i get

    { 0x00, 0x00, 0x00, 0x12 }
    Monday, November 6, 2006 2:09 PM
  • Found it!!!

    UInt32 arg = 18;
    array<Byte>^byteArray = BitConverter::GetBytes( arg);
    // {0x12, 0x00, 0x00, 0x00 }

    byteArray->Reverse(byteArray);
    // { 0x00, 0x00, 0x00, 0x12 }
    Monday, November 6, 2006 2:18 PM