27

How to Convert a byte array into an int array? I have a byte array holding 144 items and the ways I have tried are quite inefficient due to my inexperience. I am sorry if this has been answered before, but I couldn't find a good answer anywhere.

2
  • 1
    What are the ways you have tried? Commented Jun 20, 2012 at 2:57
  • Just to mention - byte is 8 bit, int is 32 bit, and you are converting a byte to int. All of the converted ints will range from 0 to 255. Commented Jul 26, 2017 at 20:16

5 Answers 5

37

Simple:

//Where yourBytes is an initialized byte array.
int[] bytesAsInts = yourBytes.Select(x => (int)x).ToArray();

Make sure you include System.Linq with a using declaration:

using System.Linq;

And if LINQ isn't your thing, you can use this instead:

int[] bytesAsInts = Array.ConvertAll(yourBytes, c => (int)c);
Sign up to request clarification or add additional context in comments.

4 Comments

I got an error: 'byte[]' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'byte[]' could be found (are you missing a using directive or an assembly reference?)
@user1166981: Make sure to reference System.Linq in your project. (Which version of the .NET framework are you targeting, by the way?)
This approach is very slow: Linq is not optimized for byte block copies. It's much faster to do int[] bytesAsInts = new int[ yourBytes.Length / 2 ]; Buffer.BlockCopy( yourBytes, 0, bytesAsInts, 0, yourBytes.Length );.
This is a very misleading answer. Typecasting each item of the byte[] will not properly cast the full 4 bytes of each int. Not sure why it is accepted.
14

I known this is an old post, but if you were looking in the first place to get an array of integers packed in a byte array (and it could be considering your array byte of 144 elements), this is a way to do it:

var size = bytes.Count() / sizeof (int);
var ints = new int[size];
for (var index = 0; index < size; index++)
{
    ints[index] = BitConverter.ToInt32(bytes, index * sizeof (int));
}

Note: take care of the endianness if needed. (And in most case it will)

2 Comments

Nice, was looking for this. People seem to forget that (int)byteVar is not the same as converting a bytearray into an int (for example 4 bytes for one int)
There is a better solution: stackoverflow.com/a/5896716/238419 That question is about int[] to byte[] but this solution should work regardless
11

Use Buffer.BlockCopy instead of Array.ConvertAll.

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

byte[] bytes = new byte[] { 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8 };
int[] ints = Array.ConvertAll(bytes, Convert.ToInt32);

will return ints[] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8}, not return ints[] = {0x04030201,0x08070605}

You should use Buffer.BlockCopy(bytes, 0, ints, 0, bytes.Length);

Comments

9

Now It's Simple like follows,

int[] result = Array.ConvertAll(bytesArray, Convert.ToInt32);

Comments

3

What you really need is:

Span<int> integerArray = MemoryMarshal.Cast<byte, int>(byteArray.AsSpan());

source -> https://stackoverflow.com/a/56029829/2791333

2 Comments

This is of course optimal from a performance point of view, but I don't think that was the questioner's intention. You are reinterpreting the byte array as an integer span. I guess the questioner wanted to have an array/span where each byte is seen as an individual integer.
This is just a cast, so if you have bytes likes 1,2,3 it will convert them into some really big integer, which might not be what you want.

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.