bit manipulation - How to do a bitwise operation on some data in C? -
i have following code snippet in c function
int i; (i = bytes; i>0; --i) { printf("byte: %d", data & 0xff); data>>= 8; }
to split given data bytes in big endian way (bytes
number bytes in data
). data - int or 100 byte long character string. however, if data
not int
, code won't work (invalid operands binary x
).
for example, assuming data
string content hello world
expect 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 simple solution wotk in pure c without libraries besides standard ones.
if want print bytes of raw binary data:
void print_bytes(const void *data, size_t len) { const unsigned char *p = data; (size_t = 0; < len; i++) printf("%d ", p[i]); }
you can call address of object, this:
unsigned long long u = 1234567890; print_bytes(&u, sizeof u); // beware of endianness! const char *s = "hello world"; print_bytes(s, strlen(s));
Comments
Post a Comment