Spaces:
Sleeping
Sleeping
File size: 1,920 Bytes
6655e35 | 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
void
hexlify (const char * tag,
const void * sp,
size_t count)
{
const uint8_t * u8p = (const uint8_t *)sp;
const uint8_t * const u8pe = u8p + count;
printf("%s: ", tag);
while (u8p < u8pe) {
printf("%02x", *u8p);
++u8p;
}
putchar('\n');
}
void
ex_struct (void)
{
struct ds {
uint8_t v;
uint32_t u32;
} st;
memset(&st, 0xbd, sizeof(st));
st.v = 1;
st.u32 = 0x12345678;
hexlify("C struct", &st, sizeof(st));
}
void
ex_packed_struct (void)
{
struct ds {
uint8_t v;
uint32_t u32;
} __attribute__((__packed__)) st;
memset(&st, 0xbd, sizeof(st));
st.v = 1;
st.u32 = 0x12345678;
hexlify("packed C struct", &st, sizeof(st));
}
void
ex_cstr (void)
{
const char str[] = "hi!";
hexlify("C string", str, 1+strlen(str));
}
void
ex_bitfield (void)
{
struct ds {
unsigned int b00l03: 3;
unsigned int b03l01: 1;
unsigned int b04l18: 24;
unsigned int b1Cl04: 4;
} st;
assert(4 == sizeof(st));
memset(&st, 0xFF, sizeof(st));
st.b00l03 = 3;
st.b04l18 = 24;
st.b1Cl04 = 4;
hexlify("bitfield lsb on le", &st, sizeof(st));
}
void
ex_arr4s16 (void)
{
int16_t arr[4] = { 1, -1, 3, -3 };
hexlify("arr[4] int16_t", arr, sizeof(arr));
}
void
ex_union4B (void)
{
struct {
uint8_t t;
union ds {
uint8_t u8[4];
int16_t s16[2];
uint32_t u32;
float f32;
} u;
} __attribute__((__packed__)) un;
un.t = 'w';
un.u.u32 = 0x12345678;
hexlify("un u32", &un, sizeof(un));
un.t = 'f';
un.u.f32 = 23.625;
hexlify("un f32", &un, sizeof(un));
memset(&un, 0xa5, sizeof(un));
hexlify("un dflt", &un, sizeof(un));
}
int
main (int argc,
char * argv[])
{
ex_struct();
ex_packed_struct();
ex_cstr();
ex_bitfield();
ex_arr4s16();
ex_union4B();
return EXIT_SUCCESS;
}
|