I am trying to convert decimal value 1 to 32 to hex value and insert it at 4th pos of char array and send it as hex value at the serial port .
My issue is it is converting to decimal to hex value but when it sends, it treat the converted hex value as char and sends it equivalent hex value.
For example
if decimal =1 then its hex = 1.
so , writebuffer[3] =1
but when we send whole writebuffer through send function, it treat this 1 as char and sends its hex value as 31. how to send its hex value.
unsignedint i=0;
char hex[3];
unsignedchar hexunsigned[3];
unsignedint dec;
do
{
unsignedchar writebuffer[8] ={0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// to place the hex value of each point on writeBuffer[3]
dec=i+1;
decimal_hex(dec,hex); //convert dec to hex value
memcpy(writebuffer+3, hex, sizeof(writebuffer+3));
unsignedshortint crc = CRC16(writebuffer, 6);
writebuffer[6] = ((unsignedchar*) &crc)[1];
writebuffer[7] = ((unsignedchar*) &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);
unsignedintchar i=0; // Note
char hex[3];unsignedchar hexunsigned[3];unsignedint dec;do
{
unsignedchar writebuffer[8] ={0x01, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
// to place the hex value of each point on writeBuffer[3]
dec=i+1;
decimal_hex(dec,hex); //convert dec to hex value
memcpy(writebuffer+3, hex, sizeof(writebuffer+3));
writebuffer[3] = i+1;
unsignedshortint crc = CRC16(writebuffer, 6);
writebuffer[6] = ((unsignedchar*) &crc)[1];
writebuffer[7] = ((unsignedchar*) &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);