Converting an Int[] to Byte[] in C#

Converting an int[] to byte[] in C#

If you want a bitwise copy, i.e. get 4 bytes out of one int, then use Buffer.BlockCopy:

byte[] result = new byte[intArray.Length * sizeof(int)];
Buffer.BlockCopy(intArray, 0, result, 0, result.Length);

Don't use Array.Copy, because it will try to convert and not just copy. See the remarks on the MSDN page for more info.

Down casting an array of Int[] into an array of Byte[]

You might think this would work:

byte[] myByteArray = myIntArray.Cast<byte>().ToArray();

But it doesnt - see Why Enumerable.Cast raises an InvalidCastException?

You can use Select though to project to a new array.

byte[] myByteArray = myIntArray.Select(i => (byte)i).ToArray();

Live demo: https://rextester.com/KVR50332

Convert listint[] to byte[]

How about this:

byte[] bytes =
lista
.SelectMany(x => x)
.SelectMany(BitConverter.GetBytes)
.ToArray();

UPDATE:

To convert the result to List<int>, you can do the following:

List<int> list =
bytes
.Select((item, index) => new {item, index})
.GroupBy(x => x.index/4)
.Select(g => g.Select(x => x.item).ToArray())
.Select(x => BitConverter.ToInt32(x, 0))
.ToList();

Convert integer to byte array

BitConverter.GetBytes(366); 

Should do the trick.

Conversion of int[] to byte[] with 1 int corresponding to 1 byte

You have to extract the low byte from every int: myByte = (byte)(myInt & 0xFF)

using System.Linq;
//...
int[] integers = {0x0004,0x0001,0x0003,0x0003};
byte[] bytes = integers.Select(n => (byte)(n & 0xFF)).ToArray();

C# Converting an int into an array of 2 bytes

You need to shift the 1st byte:

 //profile_num[0] = (byte) ((profile_number & 0xFF00));
profile_num[0] = (byte) ((profile_number & 0xFF00) >> 8);
profile_num[1] = (byte) ((profile_number & 0x00FF));

Can't convert int to byte

You're using Convert.ToInt32() when you're assigning a byte. Use Convert.ToByte() instead.

Even better would be to use TryParse instead to avoid exceptions when the string isn't valid:

byte alength;
bool success = Byte.TryParse(aa.SubString(1,aa.Length - 1), out alength);

If the parsing succeedded success will be true, otherwise false.
You can define the flow of your program depending on whether the conversion succeeds or not:

byte alength;
if(Byte.TryParse(aa.SubString(1,aa.Length - 1), out alength))
{
//Great success! continue program
}
else
{
//Oops something went wrong!
}

Int to byte array

Update for 2020 - BinaryPrimitives should now be preferred over BitConverter. It provides endian-specific APIs, and is less allocatey.


byte[] bytes = BitConverter.GetBytes(i);

although note also that you might want to check BitConverter.IsLittleEndian to see which way around that is going to appear!

Note that if you are doing this repeatedly you might want to avoid all those short-lived array allocations by writing it yourself via either shift operations (>> / <<), or by using unsafe code. Shift operations also have the advantage that they aren't affected by your platform's endianness; you always get the bytes in the order you expect them.

C# int to byte[]

The RFC is just trying to say that a signed integer is a normal 4-byte integer with bytes ordered in a big-endian way.

Now, you are most probably working on a little-endian machine and BitConverter.GetBytes() will give you the byte[] reversed. So you could try:

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

For the code to be most portable, however, you can do it like this:

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


Related Topics



Leave a reply



Submit