With the little given information, you might want to explicitly convert unsigned char
to char
.
Consider that some data may get corrupted. However, unless you really need it, you can send the data directly as signed char
. We can't know whether it's a good choice or not though.
How to read unsigned char from a socket
Question
I have to receive a UDP packet from the socket. In this packet hour, min and sec are sent as UNSIGNED CHAR. When I receive it in a char[] and put in TextBox for displaying it is not displaying the actual data which are sent, but different values.
char buffer[10];
udpSocketRxCDP->readDatagram(buffer, sizeof(buffer));
ui->textEditsec->setText(buffer[2]);
Please suggest how I can get the actual data.
Solution 2
OTHER TIPS
When you read from a socket, you are reading raw data. If you read it into a char[]
buffer and use it as-is then the data is going to be interpreted as char
. So either typecast the data to unsigned char
when needed:
ui->textEditsec->setText( (unsigned char) buffer[2] );
Or define a suitable struct
and typecast to that instead:
struct mypkt
{
unsigned char hour;
unsigned char minute;
unsigned char second;
...
};
ui->textEditsec->setText( ((mypkt*)buffer)->second );
Either way, assuming setText()
actually expects a char*
string as input, then use sprintf()
or similar function to format a string:
char str[12];
sprintf(str, "%d", (int) ((mypkt*)buffer)->second);
ui->textEditsec->setText(str);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow