【问题标题】:how to insert hex value inside an hex char array如何在十六进制字符数组中插入十六进制值
【发布时间】:2014-01-04 17:36:33
【问题描述】:

我有一个从 1 到 32 的循环。在这种情况下,1 到 32 是十进制的。我必须在 unsigned char 数组中插入 1 到 32 的十六进制值,然后执行发送。我的代码是这样的

char hex[3];
unsigned char hexunsigned[3];
int dec; 
int i=0;
do
{
    // this is the unsigned char array , i have to insert at 4th pos.  
   unsigned char writebuffer[8] ={0x01, 0x05, 0x00, 0x20, 0xFF, 0x00, 0x00, 0x00};

   // to place the hex value of each point on writeBuffer[3]
   dec=i+1;
   decimal_hex(dec,hex); //this function returns hex value of corresponding i value.
   memcpy(hexunsigned,hex,sizeof(hexunsigned)); //converting char to unsigned char

   writebuffer[3]= hexunsigned; //shows the error cannot convert from 'unsigned char [3]' to 'unsigned char'

   unsigned short int crc = CRC16(writebuffer, 6); // Calculates the CRC16 of all 8 bytes
   writebuffer[6] = ((unsigned char*) &crc)[1];
   writebuffer[7] = ((unsigned char*) &crc)[0];

   serialObj.send(writebuffer, 8);
   //send another packet only after getting the response
   DWORD nBytesRead = serialObj.Read(inBuffer, sizeof(inBuffer));
   i++;

}while(nBytesRead!=0 && i<32);

所以,这条线

writebuffer[3]= hexunsigned; 

将错误显示为无法从 'unsigned char [3]' 转换为 'unsigned char'
如何在writebuffer数组中插入hex unsigned

char hex[3];
int dec;
dec=i+1;
decimal_hex(dec,hex); 
memcpy(writebuffer+3, hex, sizeof(writebuffer+3));

被使用,它将01(dec)翻译为31。

我也试过下面的代码,它也将 01(dec) 翻译为 31。

sprintf(hex, "%X", dec); 
memcpy(writebuffer+3, hex, sizeof(writebuffer+3));

我认为它将在十六进制变量处接收到的“1”视为 ascii,并将字符“1”的十六进制值发送为 31。

发送函数如下:

void serial::send(unsigned char data[], DWORD noOfByte)
{
    DWORD dwBytesWrite;
    WriteFile(serialHandle, data, noOfByte, &dwBytesWrite, NULL);
}

【问题讨论】:

  • hexunsigned是什么类型?

标签: c++ winapi binary hex


【解决方案1】:

你可以合并这两行

memcpy(hexunsigned,hex,sizeof(hexunsigned)); //converting char to unsigned char

writebuffer[3]= hexunsigned; 

合二为一:

memcpy(writebuffer+3, hex, 3);</strike>

并且临时的十六进制符号是多余的
--------更新行--------- ----------
其实当我们说十六进制编码时,我们想用两个字节来表示一个原始字节,例如 0x33 30 // '3' '0' 代表0x30
所以在你的情况下0x01~0x20应该表示为0x00 010x02 00,如果你想使用sprintf从十进制转换为十六进制,你可以这样使用:

sprintf(hex, "%02X", dec); 
memcpy(writebuffer+3, hex, 2);

如果不考虑 CRC16,对于 i=1,您的输出将是这样的 "01 05 00 30 31 00 00 00"

【讨论】:

  • 使用上面的 memcpy 它为 i =1 发送“01 05 00 31 00 cc 9c 50”;它将 1 转换为 31 。而其他 i=2 被转换为 32。对于 i =1 它应该是 01 而不是 31。如何进行正确的转换
  • i=1 的预期输出是“01 05 00 01 00 cc 9c 50”吗? i = 12 的预期输出是多少?
  • 是的,对于 i = 12,我的预期输出是“01 05 00 01 00 cc 9c 50”,我的预期输出是 01 02 00 0C 00 CC 9C 50。我的代码将 12 视为字符并发送其十六进制值..:(
  • 如果是的话,肯定不是十六进制的,可以直接用writebuffer[3]=dec
【解决方案2】:

你必须使用 memcpy 来复制一个三字符数组。

【讨论】:

    【解决方案3】:

    hexunsigned 必须是 unsigned char 数组,而 writebuffer[3] 是 unsigned char。

    您不能将 unsigned char 分配给 unsigned char 数组。

    如果你想将 hexunsigned 复制到 writebuffer[3] 地址,请使用 memcpy。

    【讨论】:

    • 十进制值01转换为31而不是01。可能是什么原因
    • sprintf(hex, "%X", 1) 将分配 hex='1'(注意,这是一个字符 '1' 而不是数字 1),在此之后如果您使用 printf("%X",hex[0]),您将看到 31。
    猜你喜欢
    • 2018-05-17
    • 2019-07-27
    • 2018-07-26
    • 2018-01-22
    • 1970-01-01
    • 2014-03-19
    • 1970-01-01
    • 2016-08-07
    相关资源
    最近更新 更多