I'm trying to decode a 12 byte packet into a binary which is being received over UDP. The standard Ethernet Library UDP.read can only return a char array which is a problem since values over 127 could be sent. Is there a way to directly read the UDP packet?
My goal is to use bitread to decode the binary into integer variables. I feel like this should be simple and I'm way overthinking it.
The UDP packet is loaded into an array, then display it a byte at a time.
Udp.read(packetBuffer, UDP_PACKET_SIZE); // read the packet into the buffer
Serial.print(F("Received: "));
for(int i = 0; i < 12; i++)
{
Serial.print(packetBuffer[i],HEX);
Serial.print(" ");
}
The only difference between char and unsigned char is how the compiler handles a couple of operations: promoting the value to int, and right-shift >>. Of course, "promoting the vaue to int" happens whenever you do anything with the value that involves operations with ints (eg, adding 5 to it), so it's important.
To stop sign-extension, just cast the value to unsigned char before it gets promoted;
char x = 200;
int a;
int b;
a = x; // value 65480
a = (unsigned char) x; // value 200