gpt4 book ai didi

c - 十六进制到 Ascii 转换 C

转载 作者:行者123 更新时间:2023-11-30 20:10:30 25 4
gpt4 key购买 nike

我的挑战是将十六进制值转换为ascii,输入是一个指向 uint8 的指针,所以我需要逐字节转换,并且还有一个大小输入(没有字节),并且输入都是十六进制值,请帮我找出我的代码有什么问题。

输出始终为 0

uint8 ReferenceNumber[8] = {0x30,0x40,0x60,0x50,0x80,0x60,0x75,0x95};
HexToAscii(&ReferenceNumber[0], output, 8);

static void HexToAscii(uint8* input, uint8 *output, uint8 size)//No of bytes
{
uint8 i, temp;

for(i=1; i<=size; i++)
{
temp = (*(input)) >> (4*(i-1));
temp &= 0x0F;
temp +='0';
if (temp >= (10+'0'))
{
temp += ('A'-10-'0');
}
*(output+size-i) = temp;
}
}

最佳答案

声明

temp = (*(input)) >> (4*(i-1));

可以重写为

uint8 x = *(input);
temp = x >> (4 * (i - 1));

temp = input[0] >> (4 * (i - 1));

现在您可以看到,您实际上将相同的值向右移动了 0、4、8、12... 位。当将值向右移动时,您将从左侧填充 0,因此在循环 2 次迭代之后,您的 temp 变量始终为 0。

The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2^E2. If E1 has a signed type and a negative value, the resulting value is implementation-defined. - ISO/IEC 9899:TC3, Section 6.5.7: Bitwise shift operators

您需要增加您的输入指针。但是,在您的代码中,您需要为每个字节重复代码两次 - 对于 uint8 的低位和高位 4 位。

这就是我的做法(用内联函数替换宏,正如 Olaf 在 comment 中指出的那样):

/*! \brief Convert nibble (lower 4 bits) to HEX value to avoid using standard libraries.
*/
static inline __attribute__((always_inline, const)) char
NibbleToHex(uint8_t nibble) {
return ((nibble <= 9) ? ('0' + nibble) : ('A' + nibble - 10));
}

static void HexToAscii(const uint8_t *input, char *output, uint8_t size) {
while (size--) {
*(output++) = NibbleToHex((*input) >> 4u);
*(output++) = NibbleToHex((*input) & 0x0Fu);
input++; /*< Move to the next byte. */
}
}

uint8_t ReferenceNumber[8] = {0x30, 0x40, 0x60, 0x50, 0x80, 0x60, 0x75, 0x95};
HexToAscii(ReferenceNumber, output, sizeof(ReferenceNumber) / sizeof(ReferenceNumber[0]));

注意:output 必须始终是输入数据大小的两倍(假设 size 变量等于输入数据的长度)。

关于c - 十六进制到 Ascii 转换 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45232814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com