none
Convert Integer value to byte Array RRS feed

  • Question

  • Hello

     

    I have an integer value and byte array variable like this :

    int intSize = 11522;

    byte[] _Size = new byte[4];

    How can i to convert   the  integer to  byte array

    Thanks

    Sunday, June 18, 2006 8:28 AM

Answers

  • As nobugz (name says it all) noted, those are nibbles.

    for ( int i = 0; i <=3, i++ ) _Size[ i ] = (byte) ( ( intSize >> ( i * 8 ) ) & 0x000000FF );

     

    Sunday, June 18, 2006 2:15 PM
    Moderator
  • I ask you if (byte) is true

    because i want to be sure if its do not change data when it convert it

    any way the code is working

    and thank you for your help

    please can you explain to me what the

    intSize >> ( i * 8 ) ) & 0x000000FF

    do

    Monday, June 19, 2006 6:31 AM

All replies

  • one way:

    _size[0] = intSize & 0x000F;

    _size[1] = (intSize & 0x00F0) >> 8;

    _size[2] = intSize & 0x0F00 >> 16;

    _size[3] = intSize & 0xF000 >> 24;

    or

    _size[0] = intSize & 0x000F;

    _size[1] = (intSize >> 8)  & 0x000F;

    _size[2] = (intSize >> 16) & 0x000F;

    _size[3] = (intSize >> 24) & 0x000F;

    or

    for ( i = 0; i <= 3; i++)  _size[ i ] = (intSize >> ( 8 * i  ) ) & 0x000F;

    Sunday, June 18, 2006 8:57 AM
    Moderator
  • Replace the bitmasks Blair used: 0x000F -> 0xFF, 0x00F0 -> 0xFF00, 0x0F00 -> 0xFF0000 and 0xF000 -> 0xFF000000.

    Yet another way to do it which is more illustrational than practical:
        [StructLayout(LayoutKind.Explicit)]
        public struct Int2Bytes {
            public Int2Bytes(Int32 value) { b0 = b1 = b2 = b3 = 0; i32 = value; }
            [FieldOffset(0)] public Int32 i32;
            [FieldOffset(0)] public byte b0;
            [FieldOffset(1)] public byte b1;
            [FieldOffset(2)] public byte b2;
            [FieldOffset(3)] public byte b3;
        }
    ...
            static void Main()
            {
                Int2Bytes map = new Int2Bytes(0x12345678);
                Console.WriteLine("Bytes for {0:x} are [{1:x}][{2:x}][{3:x}][{4:x}]", map.i32, map.b3, map.b2, map.b1, map.b0);
            }

    Please excuse the C# syntax oopses, I'm a VB jockey...



    Sunday, June 18, 2006 10:53 AM
    Moderator
  •  

    Thank You for your help

    but the code do not work , when i press F5 the compiling

    stop and the error message is :

    Error 1 Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)

    I solve the problem by this way:

    _Size[0] = (byte)(intSize & 0x000F);

    _Size[1] = (byte)(intSize & 0x00F0);

    _Size[2] = (byte)(intSize & 0x0F00);

    _Size[3] = (byte)(intSize & 0xF000);

    so tell me is my way true ?

    Thank You Again

    Sunday, June 18, 2006 12:29 PM
  • As nobugz (name says it all) noted, those are nibbles.

    for ( int i = 0; i <=3, i++ ) _Size[ i ] = (byte) ( ( intSize >> ( i * 8 ) ) & 0x000000FF );

     

    Sunday, June 18, 2006 2:15 PM
    Moderator
  • I ask you if (byte) is true

    because i want to be sure if its do not change data when it convert it

    any way the code is working

    and thank you for your help

    please can you explain to me what the

    intSize >> ( i * 8 ) ) & 0x000000FF

    do

    Monday, June 19, 2006 6:31 AM
  • >> is the right shift bit operator

    by shifting right, by 8 times the index, we are pushing the byte we are interested to the first position, then anding by FF to get the current value for the byte array 

    • Proposed as answer by NMKha Monday, November 23, 2009 1:49 PM
    Monday, June 19, 2006 6:29 PM
    Moderator
  • just use .. BitConverter.GetBytes(integer);
    • Proposed as answer by Akshay Mahajan Thursday, April 7, 2011 4:24 PM
    Thursday, April 7, 2011 4:23 PM