gpt4 book ai didi

c - 需要转换数据(字节值转换为十六进制,十六进制字符转换为ASCII字符)

转载 作者:行者123 更新时间:2023-11-30 14:49:29 24 4
gpt4 key购买 nike

工作:我正在从内存(EEPROM/FLASH 等)读取一个字节,然后我想将该字节发送到计算机,而不是作为实际值,而是作为其十六进制值的 ascii 字符。例如,我从内存中读取了 160,即十六进制的 0xA0,现在我想将此数字发送为“A”和“0”(即 0x41 和 0x30),而不是 160,为此,我在 MPLAB IDE 中使用这种类型的 C 代码,

    //Here is the code for Parity:
uint8_t unAddParitytoByte(uint8_t unByte)
{
uint8_t unNumberofOnes = 0;
for(uint8_t unI = 0x80; unI ; unI>>=1)
{
if((unByte & unI) != 0)
{
unNumberofOnes++;
}
}
if((unNumberofOnes%2) == 0)
{
return unByte;
}
else
{
return (unByte|BIT7);
}
}
void vSendByteToSoftware(uint8_t unDataByte)
{
uint8_t unTemp = 0, unHalfByte = 0;
unTemp = (unDataByte >> 4) & 0x0F;
unHalfByte = unReturnASCII(unTemp);
/*Ignore vSerialTransmitCharacter(); as it transmit through uart and unAddParitytoByte(); to add 8th bit parity*/
vSerialTransmitCharacter(unAddParitytoByte(unHalfByte));
unBCCByte ^= unAddParitytoByte(unHalfByte);
unTemp = unDataByte & 0x0F;
unHalfByte = unReturnASCII(unTemp);
vSerialTransmitCharacter(unAddParitytoByte(unHalfByte));
unBCCByte ^= unAddParitytoByte(unHalfByte);
}
uint8_t unReturnASCII(uint8_t unNibble)
{
uint8_t unChar = 0;
switch(unNibble)
{
case 0:
unChar = '0';
break;
case 1:
unChar = '1';
break;
case 2:
unChar = '2';
break;
case 3:
unChar = '3';
break;
case 4:
unChar = '4';
break;
case 5:
unChar = '5';
break;
case 6:
unChar = '6';
break;
case 7:
unChar = '7';
break;
case 8:
unChar = '8';
break;
case 9:
unChar = '9';
break;
case 10:
unChar = 'A';
break;
case 11:
unChar = 'B';
break;
case 12:
unChar = 'C';
break;
case 13:
unChar = 'D';
break;
case 14:
unChar = 'E';
break;
case 15:
unChar = 'F';
break;
default:
break;
}
return unAddParitytoByte(unChar);
}
vSendByteToSoftware(unReadBytesfromTargetFlash());

我希望这是可以理解的。问题:我担心的是,我有一个频率为 3.6864MHz 的 Controller ,我必须在几乎 1M 字节或更多字节上执行此操作,因此非常耗时。

我想知道对于每个字节是否有先进且最快的方法来处理这个过程,这可以使我的操作变得相当快?

注意:(波特率为 115200,相当快,我想要处理字节的速度而不是发送它们的时间。)

最佳答案

每个半字节有十六个可能的值,这些值是连续的并且从零开始。这对于查找表来说是理想的选择。

    uint8_t const ascii_hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

void vSendByteToSoftware(uint8_t unDataByte)
{
uint8_t unTemp = 0, unHalfByte = 0;
unTemp = (unDataByte >> 4) & 0x0F;
unHalfByte = ascii_hex[unTemp];
/*Ignore vSerialTransmitCharacter(); as it transmit through uart and unAddParitytoByte(); to add 8th bit parity*/
vSerialTransmitCharacter(unAddParitytoByte(unHalfByte));
unBCCByte ^= unAddParitytoByte(unHalfByte);
unTemp = unDataByte & 0x0F;
unHalfByte = ascii_hex[unTemp];
vSerialTransmitCharacter(unAddParitytoByte(unHalfByte));
unBCCByte ^= unAddParitytoByte(unHalfByte);
}

更新:由于您传输的字符仅限于 16 个字符,因此您可以预先计算这 16 个字符的奇偶校验,然后使用查找表来获取奇偶校验值。 (请仔细检查我的奇偶校验计算是否正确。)

uint8_t const ascii_hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
uint8_t const ascii_hex_with_parity[16] = { 0x30, 0xB1, 0xB2, 0x33, 0xB4, 0x35, 0x36, 0xB7, 0xB8, 0x39, 0x41, 0x42, 0xC3, 0x44, 0xC5, 0xC6};

void vSendByteToSoftware(uint8_t unDataByte)
{
uint8_t unTemp = 0, unHalfByte = 0;
unTemp = (unDataByte >> 4) & 0x0F;
unHalfByte = ascii_hex[unTemp];
/*Ignore vSerialTransmitCharacter(); as it transmit through uart and unAddParitytoByte(); to add 8th bit parity*/
vSerialTransmitCharacter(unAddParitytoByte(unHalfByte));
unBCCByte ^= ascii_hex_with_parity[unTemp];
unTemp = unDataByte & 0x0F;
unHalfByte = ascii_hex[unTemp];
vSerialTransmitCharacter(unAddParitytoByte(unHalfByte));
unBCCByte ^= ascii_hex_with_parity[unTemp];
}

关于c - 需要转换数据(字节值转换为十六进制,十六进制字符转换为ASCII字符),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49587097/

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