Coder Perfect

int to byte conversion in C#

Problem

I’m looking for a way to convert an int to a byte[] BitConverter is one way to go about it. GetBytes(). But I’m not sure if that meets the following requirements:

Source: RFC1014 3.2

How might I perform an int to byte transformation that meets the aforementioned criteria?

Asked by Peter

Solution #1

The RFC simply states that a signed integer is a four-byte integer with bytes organized in big-endian order.

You’re presumably using BitConverter with a little-endian machine right now. GetBytes() returns the byte[] in reverse order. As an example, you may try:

int intValue;
byte[] intBytes = BitConverter.GetBytes(intValue);
Array.Reverse(intBytes);
byte[] result = intBytes;

However, if you want the code to be as portable as possible, you can accomplish it this way:

int intValue;
byte[] intBytes = BitConverter.GetBytes(intValue);
if (BitConverter.IsLittleEndian)
    Array.Reverse(intBytes);
byte[] result = intBytes;

Answered by paracycle

Solution #2

Another way to accomplish it is to remember that 1 byte equals 8 bits, and that a “normal” integer (int32) includes 32 bits (4 bytes). The >> operator can be used to shift bits right (the >> operator does not modify the value).

int intValue = 566;

byte[] bytes = new byte[4];

bytes[0] = (byte)(intValue >> 24);
bytes[1] = (byte)(intValue >> 16);
bytes[2] = (byte)(intValue >> 8);
bytes[3] = (byte)intValue;

Console.WriteLine("{0} breaks down to : {1} {2} {3} {4}",
    intValue, bytes[0], bytes[1], bytes[2], bytes[3]);

Answered by Maciek

Solution #3

BitConverter. GetBytes(int) almost accomplishes your goal, but the endianness is incorrect.

Before using BitConverter, you can use the IPAddress.HostToNetwork function to swap the bytes within the integer value. Use Jon Skeet’s EndianBitConverter class or GetBytes. In terms of portability, both solutions do the correct thing(tm).

int value;
byte[] bytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(value));

Answered by dtb

Solution #4

Another option is to utilize BinaryPrimitives, such as in this example.

byte[] BitConverter = intBytes int actual = BinaryPrimitives; GetBytes(123); ReadInt32LittleEndian(intBytes);

Answered by Denis

Solution #5

Why are there so many lines of code in the preceding examples…

The performance of a struct with explicit layout is unaffected.

Update: Since there has been some discussion about how to deal with endianness, I’ve provided an interface that shows how to abstract it. The reverse circumstance can be handled by another implementing struct.

public interface IIntToByte
{
    Int32 Int { get; set;}

    byte B0 { get; }
    byte B1 { get; }
    byte B2 { get; }
    byte B3 { get; }
}

[StructLayout(LayoutKind.Explicit)]
public struct IntToByteLE : UserQuery.IIntToByte
{
    [FieldOffset(0)]
    public Int32 IntVal;

    [FieldOffset(0)]
    public byte b0;
    [FieldOffset(1)]
    public byte b1;
    [FieldOffset(2)]
    public byte b2;
    [FieldOffset(3)]
    public byte b3;

    public Int32 Int {
        get{ return IntVal; }
        set{ IntVal = value;}
    }

    public byte B0 => b0;
    public byte B1 => b1;
    public byte B2 => b2;
    public byte B3 => b3; 
}

Answered by Sten Petrov

Post is based on https://stackoverflow.com/questions/1318933/c-sharp-int-to-byte