How to do a bitwise operation on some data in C?
I have the following code snippet in a C function
int i;
for (i = bytes; i>0; --i) {
printf("byte: %d", data & 0xff);
data>>= 8;
}
to split given data into bytes in a big endian way (bytes is the number
bytes in data). The data itself could be anything - an int or a 100 byte
long character string. However, if data it not an int, the code won't work
(invalid operands to binary X).
For example, assuming data is a string with the content hello world I
expect to get the following numbers:
byte: 104
byte: 101
byte: 108
byte: 108
byte: 111
byte: 32
byte: 119
byte: 111
byte: 114
byte: 108
byte: 100
I need a simple solution to wotk in pure C without any extra libraries
besides the standard ones.
No comments:
Post a Comment