none
Byte array into an array of int

    Question

  • Four byte is equal to an int, byte array into an array of int


    please verify my account


    • Edited by lctk Monday, April 10, 2017 8:32 AM
    Monday, April 10, 2017 8:31 AM

Answers

  • I want to put four byte into an int

    Example:

    int a;
    byte[] ba = new byte[] {0x1A, 0x2B, 0x3C, 0x4D };
    a = ba[3];
    a |= ba[2] << 8;
    a |= ba[1] << 16;
    a |= ba[0] << 24;
    Console.WriteLine("{0:X}", a);
    

    Output:

    1A2B3C4D                       

    - Wayne

    • Proposed as answer by Ryan Software Monday, April 10, 2017 4:54 PM
    • Marked as answer by lctk Wednesday, April 12, 2017 12:16 AM
    Monday, April 10, 2017 9:49 AM
  • Hi lctk,

    Thank you for posting here.

    Please try the following code.

     static void Main(string[] args)
            {
                // Create an Integer from a 4-byte array.
                Byte[] bytes1 = { 0xEC, 0x00, 0x00, 0x00 };
                Console.WriteLine("{0}--> 0x{1:X4} ({1:N0})", FormatBytes(bytes1),
                                                BitConverter.ToInt32(bytes1, 0));
                Console.ReadKey();
            }
            private static string FormatBytes(Byte[] bytes)
            {
                string value = "";
                foreach (var byt in bytes)
                    value += String.Format("{0:X2} ", byt);
    
                return value;
            }

    For more details, please refer to the MSDN article BitConverter.ToInt32 Method (Byte[], Int32) to

    return a 32-bit signed integer converted from four bytes at a specified position in a byte array.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by lctk Wednesday, April 12, 2017 12:16 AM
    Tuesday, April 11, 2017 7:29 AM
    Moderator
  • byte【12】=int【3】;


    Try one of multiple methods:

    byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    int[] integers = new int[bytes.Length / 4];
    
    for( int i = 0; i < bytes.Length; i += 4 )
    {
       integers[i / 4] = BitConverter.ToInt32( bytes, i );
    }

    Can be adjusted for another endianness.



    • Edited by Viorel_MVP Tuesday, April 11, 2017 8:50 AM
    • Marked as answer by lctk Wednesday, April 12, 2017 12:16 AM
    Tuesday, April 11, 2017 8:49 AM

All replies

  • I want to put four byte into an int

    please verify my account

    Monday, April 10, 2017 9:10 AM
  • At that time, I know that this is not the requirement.

    please verify my account

    Monday, April 10, 2017 9:12 AM
  • byte【12】=int【3】;

    please verify my account

    Monday, April 10, 2017 9:17 AM
  • I want to put four byte into an int

    Just stuffing 4 bytes into an int won't always give you a meaningful
    value in the int. You need to consider factors such as sign bits and
    endian-ness. {Little endian or big endian.)

    What do you plan on doing with these ints?

    What sort of data is in these bytes?

    - Wayne

    Monday, April 10, 2017 9:26 AM
  • I want to put four byte into an int

    Just stuffing 4 bytes into an int won't always give you a meaningful
    value in the int. You need to consider factors such as sign bits and
    endian-ness. {Little endian or big endian.)

    What do you plan on doing with these ints?

    What sort of data is in these bytes?

    - Wayne

    BitConverter.ToInt32() ,This method is possible,

    please verify my account

    Monday, April 10, 2017 9:34 AM
  • I need to go into an int array after drawing


    please verify my account

    Monday, April 10, 2017 9:36 AM
  • I want to put four byte into an int

    Example:

    int a;
    byte[] ba = new byte[] {0x1A, 0x2B, 0x3C, 0x4D };
    a = ba[3];
    a |= ba[2] << 8;
    a |= ba[1] << 16;
    a |= ba[0] << 24;
    Console.WriteLine("{0:X}", a);
    

    Output:

    1A2B3C4D                       

    - Wayne

    • Proposed as answer by Ryan Software Monday, April 10, 2017 4:54 PM
    • Marked as answer by lctk Wednesday, April 12, 2017 12:16 AM
    Monday, April 10, 2017 9:49 AM
  • int a;
    byte[] ba = new byte[] {0x1A, 0x2B, 0x3C, 0x4D };
    a = ba[3];
    a |= ba[2] << 8;
    a |= ba[1] << 16;
    a |= ba[0] << 24;
    Console.WriteLine("{0:X}", a);

    Output:

    1A2B3C4D                      

    Note that due to the endian scheme used on the Intel x86 family, this will
    be stored in memory in the order: 4D3C2B1A

    That is how it will appear if you were to write int 'a' to a binary file.

    To reverse the order use:

    a = ba[0];
    a |= ba[1] << 8;
    a |= ba[2] << 16;
    a |= ba[3] << 24;
    

    - Wayne

    Monday, April 10, 2017 11:27 AM
  • Hi lctk,

    Thank you for posting here.

    Please try the following code.

     static void Main(string[] args)
            {
                // Create an Integer from a 4-byte array.
                Byte[] bytes1 = { 0xEC, 0x00, 0x00, 0x00 };
                Console.WriteLine("{0}--> 0x{1:X4} ({1:N0})", FormatBytes(bytes1),
                                                BitConverter.ToInt32(bytes1, 0));
                Console.ReadKey();
            }
            private static string FormatBytes(Byte[] bytes)
            {
                string value = "";
                foreach (var byt in bytes)
                    value += String.Format("{0:X2} ", byt);
    
                return value;
            }

    For more details, please refer to the MSDN article BitConverter.ToInt32 Method (Byte[], Int32) to

    return a 32-bit signed integer converted from four bytes at a specified position in a byte array.

    I hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by lctk Wednesday, April 12, 2017 12:16 AM
    Tuesday, April 11, 2017 7:29 AM
    Moderator
  • byte【12】=int【3】;


    Try one of multiple methods:

    byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
    int[] integers = new int[bytes.Length / 4];
    
    for( int i = 0; i < bytes.Length; i += 4 )
    {
       integers[i / 4] = BitConverter.ToInt32( bytes, i );
    }

    Can be adjusted for another endianness.



    • Edited by Viorel_MVP Tuesday, April 11, 2017 8:50 AM
    • Marked as answer by lctk Wednesday, April 12, 2017 12:16 AM
    Tuesday, April 11, 2017 8:49 AM
  • This is what I mean, in accordance with the four bytes = an int, byte [12] = int [3].

    please verify my account

    Wednesday, April 12, 2017 12:17 AM