I had a data in single precision I had converted it into uint8 using type cast but now I want to change it to hex then what to do?
Afficher commentaires plus anciens
for this
y = 1.2857e-39;
y = single(y);
z = typecast(y,'uint8');
disp(z)
my result is 2 0 14 0.
Now I want to convert it into hex what to do or any other way to directly convert single precission into hex.
I have alreaty tried some number to string conversions but it won't work.
Réponses (1)
Walter Roberson
le 1 Déc 2015
sprintf('%02x', z)
5 commentaires
Nikhil Singh
le 1 Déc 2015
Nikhil Singh
le 1 Déc 2015
Walter Roberson
le 1 Déc 2015
You asked to convert your result z = 2 0 14 0 into hex; that is what the sprintf() does.
If you want to convert directly from single to hex you should consider using num2hex(y)
Nikhil Singh
le 1 Déc 2015
Walter Roberson
le 1 Déc 2015
I am not "Buddy" and I am not "dude".
a = sprintf('%02x', z)
gives me 02000e00 for a, which looks like hex to me.
a = num2hex(y)
gives me 000e0002 which again looks like hex to me. The difference compared to the typecast / sprintf() has to do with byte orders, with num2hex() putting the most significant byte first but typecast() using whatever internal order happens to be used by the processor.
If what you are looking for is not one of those two strings, then exactly what string are you looking for? Perhaps
a = sprintf('%02X ',z); disp(a)
02 00 0E 00
Catégories
En savoir plus sur Data Type Conversion dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!