[英]Need to convert data (byte value into hex and hex characters as ASCII characters)
Working: I am reading a byte from memory (EEPROM/FLASH etc) and then i want to send this byte to computer not as actual value but as a ascii characters of its hex value. 工作:我正在从内存(EEPROM / FLASH等)中读取一个字节,然后我想将此字节发送给计算机,而不是作为实际值,而是作为其十六进制值的ascii字符。 eg i read 160 from memory, which is 0xA0 in hex, now i want to send this number not as 160 but as 'A' and '0' (which are 0x41 and 0x30), for this i am using this type of c code in MPLAB IDE, 例如我从内存中读取了160,十六进制为0xA0,现在我要发送的不是160,而是“ A”和“ 0”(即0x41和0x30),为此我正在使用这种类型的c代码在MPLAB IDE中,
//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());
I hope this is understandable. 我希望这是可以理解的。 Problem: My concern is that i have a controller with frequency 3.6864MHz and i have to perform this operation on almost 1M bytes or more, so its a lot time consuming. 问题:我担心的是,我有一个频率为3.6864MHz的控制器,并且我必须在几乎1M字节或更多的字节上执行此操作,因此这非常耗时。
I was wondering if there is advance and fastest approach to this process for every single byte which can make my operation pretty fast? 我想知道对于每个字节是否有先进,最快的方法来执行此过程,这可以使我的操作非常快?
Note:(Baud rate is 115200 which is quite fast and i want speed in processing bytes rather than time to send them.) 注意:(波特率是115200,这是非常快的,我希望处理字节而不是发送它们的时间更快。)
Each half-byte has sixteen possible values which are sequential and start with zero. 每个半字节都有十六个可能的值,这些值是连续的并从零开始。 This is ideal for a lookup table. 这是查找表的理想选择。
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);
}
Update : Since the characters you are transmitting are limited to the set of sixteen, you can precalculate the parity for these sixteen characters and then use a lookup table to get the parity value. 更新 :由于您要传输的字符仅限于16个字符集,因此您可以预先计算这16个字符的奇偶校验,然后使用查找表获取奇偶校验值。 (Please double check whether I've done the parity calculations correctly.) (请仔细检查我是否正确完成了奇偶校验计算。)
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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.