1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| #include <stdio.h>
#include <arpa/inet.h>
static unsigned int get_unsigned_int_at(const char *stream, size_t size,
int offset)
{
if (offset + sizeof(unsigned int) > size) {
/* FIXME */
return -1;
}
return *((unsigned int*) &stream[offset]);
}
int main(void)
{
unsigned int i;
const char stream[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
};
i = get_unsigned_int_at(stream, sizeof(stream), 13);
printf("i=%u (0x%08x), network order:%u (0x%08x)\n", i, i,
ntohl(i), ntohl(i));
return 0;
} |
Partager