I have the following data
uint8_t d1=0x01; uint8_t d2=0x02;
I want to combine them as uint16_t
as
uint16_t wd = 0x0201;
How can I do it?
So uint8_t is the same as an 8 bit unsigned byte. The uint16_t would be the same as unsigned int on an UNO. But it's half the size of unsigned int on a Due. The point of those is that you always know exactly how big they are.
uint16_t is unsigned 16-bit integer. unsigned short int is unsigned short integer, but the size is implementation dependent. The standard only says it's at least 16-bit (i.e, minimum value of UINT_MAX is 65535 ). In practice, it usually is 16-bit, but you can't take that as guaranteed.
You can use bitwise operators:
uint16_t wd = ((uint16_t)d2 << 8) | d1;
Because:
(0x0002 << 8) | 0x01 = 0x0200 | 0x0001 = 0x0201
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With