Info
This question is closed. Reopen it to edit or answer.
Receive negative number from HC-05
1 view (last 30 days)
Show older comments
I want to send negative number from microcontroller (PIC) to Matlab. When i receive 32bit negative number, for example 0xffffff36, and try to convert it to int32 i dont get my number. Code is:
Enc2 = [Enc2,int32(bitor(bitor(Enc21,bitsll(Enc22,8)),bitor(bitsll(Enc23,16),bitsll(Enc24,24))))];
Enc21, Enc22, Enc23 and Enc24 are four bytes of 32bit number. The problem is when i try this part of code:
bitsll(Enc24,24)
I got as result 0xffffffff when i look it as binary (with function dec2bin). If i change int32 to int64, and than try to do the same, and represent it as binary it is correct, but of course it doesn't see it as correct number becouse its 64bits. What is the correct way to get correct representation of int32 number when one is received as 4 separate bytes?
0 Comments
Answers (1)
Guillaume
on 19 May 2019
I don't have the fixed-point toolbox but I would suspect that bitsll returns an 8 bit integer if the input is 8 bit.
You say you receive a 32-bit integer, in which case there's nothing to do, but later say that you get 4 bytes, so what do you get?
If you have 4 bytes, e.g.
bytes = uint8([56, 255, 255, 255]); %FFFFFF36 as hex, split into LITTLE-ENDIAN bytes
then
numberasint32 = typecast(bytes, 'int32')
If you get the bytes in Big-Endian order
bytes = uint8([255, 255, 255, 56]); %same number in Big Endian
numberasint32 = swapbytes(typecast(bytes, 'int32'))
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!