Click here to Skip to main content
15,915,724 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
lets i have a byte array.
byte[] db = System.Text.Encoding.Default.GetBytes(data);
I want to perform a operation like:
int a1=BitConverter.ToInt32(db[0]);
int a1=BitConverter.ToInt32(db[0]);
int a1=BitConverter.ToInt32(db[0]);

How can i do it.
All method i am getting is to convert an Array of Bytes to a single integer value.
But i want to convert each byte to a different int variable.
Please help me thanks in Advance.

What I have tried:

I tried some method but it was not working.
Posted
Updated 12-Jan-18 11:11am
Comments
CHill60 12-Jan-18 8:16am    
" tried some method but it was not working" - it's a good idea to show us what that method is and define what you mean by "not working"
phil.o 12-Jan-18 17:21pm    
A byte value being natively included in an integer value, I cannot see a case where you actually need this conversion. Just use the byte values in place of expected integer values, and everything will be fine.

int[] iarray = new int[db.Length];

for(int i=0;i<db.Length;i++)
  iarray[i] = (int)db[i];

This should convert your byte array into an int array...
 
Share this answer
 
v2
Comments
Jochen Arndt 12-Jan-18 8:40am    
iarray[i] = (int)db[i];

You could use linq.


C#
byte[] dbBytes = { 0x01, 0x02, 0x03, 0x04, 0x05 };
var intArray = dbBytes.Select(b =>(int)b).ToArray();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900