I wish to serial stream each byte of an unsigned integer out a data pin of a Nano. If I have an unsigned integer "x", how can I address each of the two bytes?
try something like this
int16_t x = 0x1234;
Serial.write((uint8_t*) &x, sizeof x); // GCC is little endian, so LSB will be transmitted first
uint16_t value = 1234;
byte* p = (byte*) &value;
byte firstByte = p [0]; // and so on.
(better C++ examples will be forthcoming)
did you mean byte or bit ?
Wow, thanks for the fast replies!
J-M-L: My serial routine is custom and has a 50uS bit width.
TheMembers...: Is this what you're doing? ... Casting a variable "p" as a byte array, so the two bytes of the integer can be addressed as p[0] and p[1]? I'm pretty rusty on my C handles.
This is what I was playing with. Is it legitimate?
char quotient= x/256;
char remainder= x%256;
Legitimate?
Well, it depends if "char" is signed or unsigned...
I just learned there's another easy way:
char1 = highByte(value);
char2 = lowByte(value);
Thanks for your help....
1 Like
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.