编译断言

编译断言

编译期间断言判断

利用枚举数判断

1
2
3
4
5
6
7
8
9
10
#define COMPILER_CONCAT_(a, b) a##b
#define COMPILER_CONCAT(a, b) COMPILER_CONCAT_(a, b)

// Divide by zero if the the expression is false. This
// causes an error at compile time.
//
// The special value '__COUNTER__' is used to create a unique value to
// append to 'compiler_assert_' to create a unique token. This prevents
// conflicts resulting from the same enum being declared multiple times.
#define COMPILER_ASSERT(e) enum { COMPILER_CONCAT(compiler_assert_, __COUNTER__) = 1/((e) ? 1 : 0) }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//(4位)单hex转字符
static char hex_to_ascii(uint8_t x)
{
return ('0' + (x>9 ? x+0x27 : x));
}

//8位HEX转字符串
uint32_t util_write_hex8(char *str, uint8_t value)
{
static const char nybble_chars[] = "0123456789abcdef";
*(str + 0) = nybble_chars[(value >> 4) & 0x0F ];
*(str + 1) = nybble_chars[(value >> 0) & 0x0F ];
return 2;
}

//字符转数
static uint8_t ctoh(char c)
{
return (c & 0x10) ? /*0-9*/ c & 0xf : /*A-F, a-f*/ (c & 0xf) + 9;
}
-->

请我喝杯咖啡吧~

支付宝
微信