2

I want to create a 32 bit integer programmatically from four bytes in hex such as:

Lowest byte is AA

Middle byte is BB

Other middle byte is CC

Highest byte is DD

I want to use the variable names for that where:

byte myByte_1 = 0xAA
byte myByte_2 = 0xBB
byte myByte_3 = 0xCC
byte myByte_4 = 0xDD

So by using the above bytes and using bitwise operations how can we obtain: 0xDDAABBCC ?

4
  • 3
    Pretty sure it's just BitConverter.ToInt32. Seeing if there's a good duplicate for that (in c#) Commented Dec 25, 2021 at 19:59
  • You might want to look at stackoverflow.com/questions/6165171/convert-byte-array-to-int Commented Dec 25, 2021 at 20:00
  • 2
    int result = unchecked((myByte_4 << 24) | (myByte_3 << 16) | (myByte_2 << 8) | myByte_1); Commented Dec 25, 2021 at 20:02
  • Ok I understood now it works for me. Thank you all. Should I delete the question ? Commented Dec 25, 2021 at 20:18

2 Answers 2

5

You can construct such int explicitly with a help of bit operations:

int result = myByte_4 << 24 | 
             myByte_3 << 16 | 
             myByte_2 << 8 | 
             myByte_1;

please, note that we have an integer overflow and result is a negative number:

Console.Write($"{result} (0x{result:X})");

Outcome;

-573785174 (0xDDCCBBAA)

BitConverter is an alternative, which is IMHO too wordy:

int result = BitConverter.ToInt32(BitConverter.IsLittleEndian 
  ? new byte[] { myByte_1, myByte_2, myByte_3, myByte_4 }
  : new byte[] { myByte_4, myByte_3, myByte_2, myByte_1 });
Sign up to request clarification or add additional context in comments.

Comments

0

We can do this by asking C# to make us a struct that shares memory space for its members:

using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Explicit)]
struct Bynteger{

    [FieldOffset(0)]
    public byte Lowest;
    [FieldOffset(1)]
    public byte Middle;
    [FieldOffset(2)]
    public byte OtherMiddle;
    [FieldOffset(3)]
    public byte Highest;

    [FieldOffset(0)]
    public int Int;

    [FieldOffset(0)]
    public uint UInt;

}

If you assign to those bytes then read the int or the uint you'll get the value:

var b = new Bynteger{
    Lowest = 0xAA,
    Middle = 0xBB,
    OtherMiddle = 0xCC,
    Highest = 0xDD
}; 

Console.WriteLine(b.Int);                //prints -573785174
Console.WriteLine(b.UInt);               //prints 3721182122
Console.WriteLine(b.UInt.ToString("X")); //prints 0xDDCCBBAA

how can we obtain: 0xDDAABBCC ?

Er.. If 0xDD AA BB CC isn't a typo (did you mean to say 0xDD CCBBAA`?), you can jiggle the layout around by changing the number in the FieldOffset, e.g.:

[FieldOffset(2)]
public byte Lowest;
[FieldOffset(1)]
public byte Middle;
[FieldOffset(0)]
public byte OtherMiddle;
[FieldOffset(3)]
public byte Highest;

Will give you the 0xDDAABBCC you requested, if you keep the assignments you said (Lowest = 0xAA etc)

You're free to name the fields however you like too.. (perhaps B1 to B4 ?)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.