1

hi i have this kind of array

int[] arrayint = new int[32];

and it contains

arrayint[0] = 99
arrayint[1] = 121
arrayint[2] = 99
arrayint[3] = 66
...

is there a simple way to copy that integer array into a byte array like i want to make this byte array

byte[] streambit;

and it should be the same to the arrayint value

i want to be like this the output

streambit[0] = 99
streambit[1] = 121
streambit[2] = 99
streambit[3] = 66
...

3 Answers 3

5
streambit = arrayint.Select(i => (byte)i).ToArray();

Just make sure that you have no value greater than 255.

Sign up to request clarification or add additional context in comments.

4 Comments

You may like to add that no negative values should be there as well as corresponding byte will not be 'same' in that case...
An interesting thing to note here is that any integers greater than 255 or less than 0 will be converted in a wrapping fashion without throwing an overflow exception. For example: -1 will convert to 255 and 256 will convert to 0.
that's it thank you very much... can you explain me about the code? i kind of confused about the meaning of i => (byte)i).ToArray() how does that make byte?
i => (byte)i is called a lambda expression, a special notation for an anonymous function. In this case, it's a function taking a parameter, converting it to int, then returning it. The Select method takes every element of the collection, then call the lambda on them. The ToArray method copies the result to an array.
2

Without LINQ (useful when targeting .Net 2.0 for instance):

byte[] bytearray = Array.ConvertAll<int, byte>(arrayint, (z) => (byte)z);

Well yeah, much more faster than LINQ:

Test code (could be improved, but this gives an idea):

private static void Main(string[] args)
{
    int[] arrayint = new int[40000];

    arrayint[0] = 99;
    arrayint[1] = 157;
    arrayint[2] = 1;
    arrayint[3] = 45;

    byte[] bytearray;

    Stopwatch sw = Stopwatch.StartNew();
    for (int i = 0; i < 10000; i++)
    {
        bytearray = Array.ConvertAll<int, byte>(arrayint, (z) => (byte)z);
    }

    sw.Stop();
    Console.WriteLine("ConvertAll took {0} ms", sw.ElapsedMilliseconds);

    sw = Stopwatch.StartNew();
    for (int i = 0; i < 10000; i++)
    {
        bytearray = arrayint.Select(z => (byte)z).ToArray();
    }

    sw.Stop();
    Console.WriteLine("LINQ took {0} ms", sw.ElapsedMilliseconds);

    Console.ReadLine();
}

Result:

ConvertAll took 1865 ms

LINQ took 6073 ms

1 Comment

First time I hear of Array.ConvertAll. Seems indeed a better choice. When executing the Linq query, the results are stored in an internal buffer, then copied to an array. Array.ConvertAll directly allocates an array and makes the conversion during the copy. It probably explains the huge performance improvement.
0
streambit=arrayint.Where(x=>x>=0&&x<=255).Select(y=>(byte)y).ToArray();

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.