How to convert integer to 12 bit binary and vise versa
46 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello, I want to convert integers in the range -400 to +800 to 12 bit binary and vice versa.
dec= -333;
a= decimalToBinaryVector(typecast(int16(dec),'uint16'),16);
str_x = num2str(a);
b=typecast(uint16(bin2dec(str_x)),'int16')
The above code gives me 16 bit Binary value. I want to convert the integer to 12 bit binary and vice versa.
2 commentaires
Walter Roberson
le 5 Mar 2020
Why? Your code at https://www.mathworks.com/matlabcentral/answers/508682-how-to-pass-binary-values-to-mex already does 12 bit conversion.
Réponse acceptée
Walter Roberson
le 5 Mar 2020
Modifié(e) : Walter Roberson
le 5 Mar 2020
mod(typecast(int16(dec), 'uint16'), 4096)
and convert to binary.
Or
bitget(int16(dec), 12:-1:1)
which does the binary conversion. You might want to double() the output for your purposes.
4 commentaires
Walter Roberson
le 5 Mar 2020
b = int16(sum(bitset(0,12:-1:1,a)));
if b > 2047; b = b - 4096; end
Plus de réponses (1)
Steven Lord
le 5 Mar 2020
There's no int12 data type in MATLAB. Depending on what you want to do with this data, if you have Fixed-Point Designer you could store it as an fi object. See this documentation page for the basics of how to work with fi objects.
>> A = sfi(10, 12, 0)
A =
10
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 12
FractionLength: 0
>> bin(A)
ans =
'000000001010'
Voir également
Catégories
En savoir plus sur Create Fixed-Point Objects in MATLAB dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!