Hello,
I am working with an Arduino Leonardo ETH. I am receiving data via ethernet:
...
if ( (client) && (client.available()) > 5)
{
byte buf[4] = { 0,0,0,0 };
client.readBytesUntil
client.readBytes(buf, 4);
proccessArray(buf);
Ethernet.maintain();
}
...
So far, so good. However, now the last two bytes of my buffer array should be converted to a uint16_t. The data is sent with the most significant byte first (big-endian order), but in principle I could change that as I am writing the sending app too. However, if possible, I'd keep it like that.
Now how do I convert that to an uint16_t?
I know, it's quite basic stuff, but I am chemist doing automatization... So hopefully it's forgivable... 
Thanks!
Best,
Andreas
uint16_t myInt = buf[3] << 8 | buf[4]; // shift one byte by 8 and OR the two to get your result .
kind-of-thing...
but I'm not sure if the endianness is correct...
Hello,
thanks for the help: To test if the endianness is correct, I can just reverse buf[3] and buf[4] in the code, right?
And: It should be byte[3] and byte[2], right? Indices start with zero...?
Thanks,
Andreas
andreas_:
Hello,
And: It should be byte[3] and byte[2], right? Indices start with zero...?
Thanks,
Andreas
yeah... I'm in and out of Lua these days 
Well...I better ask. I have to do quite a lot of things in MATLAB and there indices do start with 1...
I hope it's OK to ask a follow up question: Reversing the same thing, meaning creating a byte[2] of an uint16_t would then be:
uint16_t val = 65535;
byte low = val & 0xff;
byte high = (val >> 8) & 0xff;
byte[2] = {high, low}; // Order for Big Endian;
Or is there a more efficient way?
Best,
Andreas
andreas_:
byte high = (val >> 8) & 0xff;
Or is there a more efficient way?
the low order bits of val end up pushed off into the trash bin, so no need to & 0xFF...