Reputation: 207
I want to convert some bytes to an int. This is my code so far:
unsigned char *bytePtr = (unsigned char *)[aNSDataFrame];
I want to take 4 bytes from this unsigned char:
myFrame[10]
, myFrame[11]
, myFrame[12]
and myFrame[13]
and convert them to an integer.
Upvotes: 0
Views: 1039
Reputation: 3162
you can do,
int a;
a=myframe[10];
a=a<<8;
a=a|myframe[11];
a=a<<8;
a=a|myframe[12];
a=a<<8;
a=a|myframe[13];
this will create integer containing those bytes
Upvotes: 1
Reputation: 64
int bytesToInt(unsigned char* b, unsigned length)
{
int val = 0;
int j = 0;
for (int i = length-1; i >= 0; --i)
{
val += (b[i] & 0xFF) << (8*j);
++j;
}
return val;
}
Upvotes: 0