locked
Converting a decimal to a byte array RRS feed

  • Question

  • Hello everyone,

    I am trying to convert an int to a byte array. What would be the easiest way to do this.

    I tried BitConverter.GetBytes(12);
    but the byte array is as follows
    byte[0] = 12
    byte[1] = 0
    byte[2] = 0
    byte[3] = 0

    I do not want the zeros to appear what can I do?

    Thanks
    Wednesday, August 30, 2006 10:49 AM

Answers

  • An integer is 32 bits long or 4 bytes long, what you are seeing is a complete representation of it.
    If you do not want the zero's you've got a couple of options... if your integer is smaller than 255 you can manually put it into a single index of a byte array (or even just a single byte variable that is not part of an array).

    If it is bigger than 255 then you’d perhaps do some bitshifting to manually size it but this is problematic because you wouldn’t be able to pull it out again without those extra zeros... take the following code where we manually typecast the variable containing the number to a short (really the smallest type we can convert with BitConverter) and then display it’s byte array values:

             int val = 12;

             byte[] b = BitConverter.GetBytes((short)val);

             Console.WriteLine(BitConverter.ToString(b));
             Console.WriteLine(BitConverter.ToInt32(b, 0));

    This code would display 0C-00 as the result which would be expected, the problem is that when we hit line #4 we get an exception that tells us:

          Destination array is not long enough to copy all the items in the collection. Check array index and length.

    In this case you need all of those extra, zeros and if you remove the typecast to short from above and let it use all for byte values then things work just fine.

    Again, if you do not want to have those extra bytes you’ll need to do the packaging into a byte array yourself.

    Wednesday, August 30, 2006 11:27 AM

All replies

  • An integer is 32 bits long or 4 bytes long, what you are seeing is a complete representation of it.
    If you do not want the zero's you've got a couple of options... if your integer is smaller than 255 you can manually put it into a single index of a byte array (or even just a single byte variable that is not part of an array).

    If it is bigger than 255 then you’d perhaps do some bitshifting to manually size it but this is problematic because you wouldn’t be able to pull it out again without those extra zeros... take the following code where we manually typecast the variable containing the number to a short (really the smallest type we can convert with BitConverter) and then display it’s byte array values:

             int val = 12;

             byte[] b = BitConverter.GetBytes((short)val);

             Console.WriteLine(BitConverter.ToString(b));
             Console.WriteLine(BitConverter.ToInt32(b, 0));

    This code would display 0C-00 as the result which would be expected, the problem is that when we hit line #4 we get an exception that tells us:

          Destination array is not long enough to copy all the items in the collection. Check array index and length.

    In this case you need all of those extra, zeros and if you remove the typecast to short from above and let it use all for byte values then things work just fine.

    Again, if you do not want to have those extra bytes you’ll need to do the packaging into a byte array yourself.

    Wednesday, August 30, 2006 11:27 AM
  • Thanks for that reply.

    I ended up packaging the int into a byte array by myself.


    Wednesday, August 30, 2006 2:13 PM