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?

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)

sprintf('%02x', z)

5 commentaires

Buddy give some detail ,is it for direct conversion from single to hex or from uint8 to hex
I tried in following manner a = sprintf('%02x',z); disp(a)
Result: 02000e00
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)
dude its not working for my data also, you just try it once and share the results.
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

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by