我有一個循環讓說從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(分解)爲31.
我曾嘗試下面的代碼還,還平移01(分解)爲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'是什麼類型? – kmort
我更新了代碼 – user3048644