How do i convert decimal to hexadecimal for floating points

I have attached the screenshots, in which i have extracted HOG features for a image and found its values in floating point but when i am converting the same decimal values to hexadecimal i am getting the error. please help

8 commentaires

Stephen23
Stephen23 le 20 Juil 2017
Modifié(e) : Stephen23 le 20 Juil 2017
There is no single "standard" way of encoding values with fractional values using binary/hexadecimal. First you need to specify which of the many possible encoding methods you want to use, and then obtain/implement a function for that encoding.
So, how do you want to encode your decimal fractions?
i am unable to understand what encoding methods you are talking about but i need to use the Hexadecimal values for the FPGA board hence i need to extract HOG features for the image and as i am getting the values in the floating points example: 0.1089 0.1139 0.0825 0.1183 0.1202 0.2336 0.2615 i need to convert these values in Hexadecimal.
Can anyone help me with the conversion please
Which FPGA are you using that supports double precision numbers? I did encounter one FPGA a couple of months ago that supported single precision, but not double precision.
When you program an FPGA you are almost always using Fixed Point. The method for converting Fixed Point to hex are a bit different than what has been posted.
i am using ARTIX-7 Xilinx,
i am using Face recognition on ANN on FPGA platform, for that i have extracted HOG features but i am unable to give it as a input to the .coe file as it is not getting converted to hexadecimal.
@Walter,
As far as I know, all Xilinx FPGAs support single precision (and half-precision) floating-point (but not double). I commonly use single precision for some operations on my national instruments c-rio fpgas (programmed in labview). It does use a ton of gate and requires a lot more clock cycles than fixed-point though.

Connectez-vous pour commenter.

 Réponse acceptée

David Goodmanson
David Goodmanson le 19 Juil 2017
HI Tousif,
try num2hex.

6 commentaires

not helping sir
num2hex is what is appropriate for the requirements you have defined.
ARTIX-7 Xilinx uses IEEE 754 single precision, it appears. Use
num2hex(single(TheData))
Note: this will output in Big Endian. For example, for
>> num2hex(single(pi))
ans =
'40490fdb'
the '40' together with the first bit of the following '4' part together encode the sign bit and the exponent. The internal order for little-endian systems would correspond to 'db0f4940'. Depending on whether the Xilinx is big-endian or little-endian you might need to manipulate the order of the bytes.
sir thank you very much for your help, its working but the bit here is 8 bit "40490fdb", how can i change it to 2 bit? Is there any way for the conversion?
how it can be changed to 8 bit from 32 bit?
reshape((dec2bin(typecast(single(pi), 'uint8')) - '0').', 1, [])
Note that this would be in little-endian order. You might prefer
reshape((dec2bin(typecast(swapbytes(single(pi)), 'uint8')) - '0').', 1, [])

Connectez-vous pour commenter.

Plus de réponses (2)

You can simply write your own floating point to hex converter, like the following:
x = 5329.675; % Floating point number
xI = floor(x); % Integer Part
HI = dec2hex(xI); % Integer Part in Hex
xF = x - xI; % Floating part
HF = ''; % Floating part in Hex (begining)
ii = 0;
while xF > 0
ii = ii + 1;
ff = xF * 16^ii;
II = floor(ff);
HF = [HF, dec2hex(II)];
xF = ff - II;
end
x_hex = [HI,'.',HF] % Concatinate both Integer and Floating Part in HEX format

1 commentaire

Unfortunately, this code works with errors. If we try to convert 0.45619303 answer is: '0.74C911835D980', but correct answer is 0.74C91100835D98. And another example: 0.38574379 -> '0.62C1AE2AF622C0', but correct answer is 0.62C01AE2AF622DC. As we can see, your function loses zeros in the hex fractional part.
Where is the mistake? I can't understand.

Connectez-vous pour commenter.

Giancarlo Soasti
Giancarlo Soasti le 25 Oct 2018
dec2hex is the function you are probably looking for

2 commentaires

No, dec2hex only works on nonnegative integers. If you look at the images the original poster posted, they need to convert floating point numbers including fractions.
Yep. You are right. I should have paid attention to the images.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by