Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Convert a byte array into an int array?

Tags:

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.

like image 974
user1166981 Avatar asked Jun 20 '12 02:06

user1166981


People also ask

Can we convert byte to int in Java?

The intValue() method of Byte class is a built in method in Java which is used to return the value of this Byte object as int.

How do I convert an integer to a byte array?

The Ints class also has a toByteArray() method that can be used to convert an int value to a byte array: byte[] bytes = Ints. toByteArray(value);

How do you convert an array to int in Java?

We can use the parseInt() method and valueOf() method to convert char array to int in Java. The parseInt() method takes a String object which is returned by the valueOf() method, and returns an integer value. This method belongs to the Integer class so that it can be used for conversion into an integer.

How do you convert Bytearray to int in Python?

To convert bytes to int in Python, use the int. from_bytes() method. A byte value can be interchanged to an int value using the int. from_bytes() function.


1 Answers

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); 
like image 118
vcsjones Avatar answered Sep 18 '22 03:09

vcsjones