简体   繁体   中英

How to divide a number into 1 byte chunks to be sent through serial bluetooth in c++

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: 将二进制 725 分成两个字节的示例

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

Something like:

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM