How to cast two uint8 into int16 in Simulink?
Afficher commentaires plus anciens
I have two uint8 as input of my system. Together, they represent an int16. How can I cast these two values into an int16? In MATLAB, I would convert them to uint16, shift the bits of the first one 8 to the left and the use bitwise OR to get the bit representation and finally apply a typecast to reinterpret the bits (with respect to two's complement). In MATLAB it works:
raw = uint8([255,255])
raw = uint16(raw)
myint16 = typecast(bitor(bitshift(raw(1),8), raw(2)), 'int16') %myint16 = -1
How do I implement this in Simulink? I am restricted to the slcilib (for Code Inspector Compliance). That's why I cannot use the typecast function. Finally, I found a solution, but it's a really complicated if-then-else stuff, with around four casts. Is there another easy way?
Réponses (1)
Fangjun Jiang
le 31 Août 2015
1 vote
1. Use the ShiftArithmetic block in Simulink->Logic and Bit Operations.
2. Multiply the first number with 256 and then add to the second number.
5 commentaires
Markus
le 31 Août 2015
Walter Roberson
le 31 Août 2015
To produce a signed value, convert to unsigned in myuint16 and then
if myuint16 < 32768
myint16 = int16(myuint16);
else
myint16 = -int16(bitwise_not(myuint16)) - int16(1)
end
for appropriate bitwise_not routine
Fangjun Jiang
le 31 Août 2015
Modifié(e) : Fangjun Jiang
le 1 Sep 2015
Would this be correct?

Walter Roberson
le 31 Août 2015
I am not sure of the purpose of the bitwise OR ?
Catégories
En savoir plus sur Signal Import and Export dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!