我希望有人向我解释一下我工作中代码库中的奇怪功能。
bool uint32tox(const UINT32 input, UINT8 *str)
{
UINT32 val = input;
INT16 i;
UINT16 j = 0;
if (str == NULL) return(false);
for (i = 28; i >= 0; i -= 4) {
UINT8 sval = (val >> i) & 0x0F;
str[j++] = (sval < 10u) ? sval + '0' : sval - 10 + 'A';
}
str[j] = '\0';
return(true);
}
为什么和0x0F一起,为什么我从28开始。
答案 0 :(得分:2)
我冒昧地对代码进行了一些评论
/*
Convert an unsigned 32-bit (assuming UINT32 to mean uint32_t or similar) large
integer to a hexadecimal representation
*/
bool uint32tox(const UINT32 input, UINT8 *str)
{
UINT32 val = input;
INT16 i;
UINT16 j = 0;
if (str == NULL) return(false);
for (i = 28; i >= 0; i -= 4) {
// Shift input by i bits to the right and snip of the rightmost 4 bits
/*
0 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
with i = 28 sval contains the leftmost four bits 28 - 31
with i = 24 sval contains bits 24-27
with i = 20 sval contains bits 20-23
with i = 16 sval contains bits 16-19
with i = 12 sval contains bits 12-15
with i = 8 sval contains bits 8-11
with i = 4 sval contains bits 4- 7
with i = 4 sval contains bits 0- 3
*/
UINT8 sval = (val >> i) & 0x0F;
// If sval is smaller than ten we can use a base ten digit
// that gets constructed by adding the numerical value of ASCII '0'
// to sval (the digits 0-9 are guaranteed to be in order without gaps).
// If sval is bigger we need a hexdigit, one of A, B, C, D, E, F.
// These are normally consecutive at least in ASCII, so you can handle it
// like the other branch and just add the numerical value of 'A' instead
str[j++] = (sval < 10u) ? sval + '0' : sval - 10 + 'A';
}
// terminate the UINT8 array such that it can be used as a C-string
str[j] = '\0';
return(true);
}
答案 1 :(得分:1)
首先,这个函数被破坏了,因为它没有* str缓冲区的大小,可能导致恶意攻击或者纯粹的bug溢出。
其次,您可以使用sprintf(strOut, "%x",input);
代替此功能。
有一个技巧,其中每个十六进制数字是4个二进制位位:
0100 1111 0010 1101 0011 1110 1111 0000 >> 83,022,831
4 =4 15=F 2 =2 13=D 3 =3 14=E 15=F 0 =0 >> 0x4F2D3EF
i=28
i=0
被处理。