0

I am trying to send some data with BLE from an STM32 board to an Raspberry Pi 3B board. However, the "write" function of BLE API only supports uint8_t array transfer, but the data I need to send include both positive and negative numbers such as 42 and -283. The original values are stored in an int16_t variable, and I need to parse them into the uint8_t array before transfer them. I just want one int16_t be put in an array, so it can take use more that one slot in the array if needed. For example, 283 can be stored as 1,1B.

Does anyone know how to do that? I guess I can use >>8 to do it, but how about the negative numbers?

4
  • 5
    Are you familiar with 2-s complement arithmetic, that's used to represent signed numbers in all computers? Do you understand how it works, and how to use it? Commented Oct 27, 2022 at 13:12
  • @SamVarshavchik Do you mean flipping the 0 and 1 in the binary based number? If so, then I guess I have to parse the number again(flip back) after receiving? Commented Oct 27, 2022 at 13:22
  • 4
    Your question is basically about how to serialize data. Likely the safest "manual" way in C++ is to employ memcpy. Better, use any decent serialization library. Commented Oct 27, 2022 at 13:25
  • No, 2-s complement is not about flipping the 0 and 1, anywhere. This is how computers represent negative numbers. If you are familiar with 2-s complement arithmetic and how this translates to representing integer values, whether they are 8, 16, 32, or 64 bits, then you need to clarify, exactly, what your specific question is. If you are not familiar with these topics you can find them explained and discussed in many introductory textbooks on computer science and programming. Unfortunately, Stackoverflow is not a replacement for a textbook, we only answer specific questions here. Commented Oct 27, 2022 at 16:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.