How to divide a number into 1-byte chunks to be sent through serial Bluetooth in C++?

Viewed 99

I want to send a number through serial Bluetooth from an Arduino to an Android application.

I've managed to send 1 byte using the following code in C++:

int number = 255;          
SerialBT.write((uint8_t*)&number,1);
SerialBT.flush();

This works fine, but since Arduino won't admit int16 or any other pointer bigger than 1 byte, I need to "chunk" the number down to 8-bit parts, and send each "chunk" at a time.

For example: if I need to send the number "725", which is 10 1101 0101 in binary, I would need to send 2 bytes:

Example division of binary 725 into two bytes

How can I divide a number into "8 bit chunks"?

1 Answers

Something like this:

uint16_t number = 1000;
uint8_t lByte = number & 0b11111111;
uint8_t hByte = number >> 8;

There are also lowByte() and highByte() functions available, but I don't know their exact behaviour.

But why not use SerialBT.print() or SerialBT.println() and convert what you receive into a number?

Related