5

I have a string of 256*4 bytes of data. These 256* 4 bytes need to be converted into 256 unsigned integers. The order in which they come is little endian, i.e. the first four bytes in the string are the little endian representation of the first integer, the next 4 bytes are the little endian representation of the next integer, and so on.

What is the best way to parse through this data and merge these bytes into unsigned integers? I know I have to use bitshift operators but I don't know in what way.

13
  • "but i don't know in what way" - you read up on how shifting operators work and hopefully you will instantly know how. Commented Jul 29, 2013 at 5:09
  • the string is just passed through via a redirected file. The first 256*4 bytes are the little endian encodings of 256 unsigned integers. I need to convert each 4 bytes into an unsigned integer and store it in an array. What i dont know how to do is merge each set of 4 bytes into an unsigned int. Commented Jul 29, 2013 at 5:10
  • @user0123 byte0 | (byte1 << CHAR_BIT) | (byte2 << 2 * CHAR_BIT) | (byte3 << 3 * CHAR_BIT)... Commented Jul 29, 2013 at 5:10
  • @H2CO3 - I have read far and wide on google about how the bitshifting operators work, including the & and | operators. I am still extremely confused on how to merge 4 bytes into an unsigned int Commented Jul 29, 2013 at 5:10
  • @user0123 Just like my comment above ^^ explains it. Commented Jul 29, 2013 at 5:11

3 Answers 3

6

Hope this helps you

unsigned int arr[256]; char ch[256*4] = "your string"; for(int i = 0,k=0;i<256*4;i+=4,k++) { arr[k] = ch[i]|ch[i+1]<<8|ch[i+2]<<16|ch[i+3]<<24; } 
Sign up to request clarification or add additional context in comments.

12 Comments

what if host system is big endian?
@thomas This approach is endian-agnostic. It does not matter what the host system's endianness is.
@Saksham Don't believe the false positive ;)
@thomas If the host system is big-endian, then this will work just like it would work on a little-endian system. (And not doing what OP wanted, and invoking undefined behavior by shifting stuff into the sign bit of the resulting integer...)
@Saksham This solution is almost correct, you should just change int to unsigned int. (Edit: done by OP, +1.)
|
4

Alternatively, we can use C/C++ casting to interpret a char buffer as an array of unsigned int. This can help get away with shifting and endianness dependency.

#include <stdio.h> int main() { char buf[256*4] = "abcd"; unsigned int *p_int = ( unsigned int * )buf; unsigned short idx = 0; unsigned int val = 0; for( idx = 0; idx < 256; idx++ ) { val = *p_int++; printf( "idx = %d, val = %d \n", idx, val ); } } 

This would print out 256 values, the first one is idx = 0, val = 1684234849 (and all remaining numbers = 0).

As a side note, "abcd" converts to 1684234849 because it's run on X86 (Little Endian), in which "abcd" is 0x64636261 (with 'a' is 0x61, and 'd' is 0x64 - in Little Endian, the LSB is in the smallest address). So 0x64636261 = 1684234849.

Note also, if using C++, reinterpret_cast should be used in this case:

const char *p_buf = "abcd"; const unsigned int *p_int = reinterpret_cast< const unsigned int * >( p_buf ); 

Comments

0

If your host system is little-endian, just read along 4 bytes, shift properly and copy them to int

char bytes[4] = "...."; int i = bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24); 

If your host is big-endian, do the same and reverse the bytes in the int, or reverse it on-the-fly while copying with bit-shifting, i.e. just change the indexes of bytes[] from 0-3 to 3-0

But you shouldn't even do that just copy the whole char array to the int array if your PC is in little-endian

#define LEN 256 char bytes[LEN*4] = "blahblahblah"; unsigned int uint[LEN]; memcpy(uint, bytes, sizeof bytes); 

That said, the best way is to avoid copying at all and use the same array for both types

union { char bytes[LEN*4]; unsigned int uint[LEN]; } myArrays; // copy data to myArrays.bytes[], do something with those bytes if necessary // after populating myArrays.bytes[], get the ints by myArrays.uint[i] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.