Matlab convert form hex to float - matlab

I'm working with a device that send to me hex values, and I need convert those values to his real float value. Someone know how to convert from hex values to float in matlab?
Thx

Take a look at hex2dec, to convert your hex to decimal.
Hex format is inherently integer (the floating point position is not defined), so you will have to give more info: Does the hex represent a mantissa-exponent floating point number? Does it represent a fixed-point number?

the hex represent a mantissa-exponent floating point number. for exemple 0x44ADE000 equal to 1391.0

Related

Convert Scientific Notation to decimal in Azure Data Flows

I have an input file that has a column with mixture of decimal and scientific notation values. I have tried to convert the column using toDecimal, which seems to work for the non-zero decimals and the scientific notation values, but zeroes are converted to scientific notation. Is it possible to keep the zero values as plain zeroes?
Looks like by default 0/0.0 is converted to exponential value when using the decimal function. One way is to convert decimal to string.
case(Value == 0.0, toString(0.0),toString(toDecimal(Value,20,10)))

converting 64 bits binary 1D vector into corresponding floating and signed decimal number

Please let me know how to achieve this as I tried a lot but didn't get the desired result for vector, u=[0;1;0;0;1;1;0;0;0;0;1;1;0;1;1;1;1;0;0;0;1;0;0;1;0;1;0;0;0;0;0;1;0;1;1;0;0;0;0;0;0;0;0;0;1;1;0;1;0;1;0;1;1;0;1;1;1;1;0;0;0;0;0;0];
desired output=-108.209
Regards
Nitin
First off, I think your expectation for a correct answer is off. The first bit in a double is the sign. So if you're expecting a negative number, the first bit should be 1. Even if you had your bits backward, it's still a leading 0. There are all sorts of binary to float calculators if you search for them. Here's an example:
http://www.binaryconvert.com/result_double.html?hexadecimal=4C378941600D5BC0
To answer your question for how to do this in Matlab, Matlab's built in function for converting from binary is bin2dec. However, it's expecting a char array as an input, so you'll need to convert to char with num2str. The other trick here is that bin2dec only supports up to 53 bits. So you'll need to break it into two 32 bit numbers. The last piece of the puzzle is to use typecast to convert your pair of 32bit integers into a double. Put it all together, and it looks like this:
bits = [0;1;0;0;1;1;0;0;0;0;1;1;0;1;1;1;1;0;0;0;1;0;0;1;0;1;0;0;0;0;0;1;0;1;1;0;0;0;0;0;0;0;0;0;1;1;0;1;0;1;0;1;1;0;1;1;1;1;0;0;0;0;0;0];
int1 = uint32(bin2dec(num2str(bits(1:32)')));
int2 = uint32(bin2dec(num2str(bits(33:64)')));
double_final = typecast([int2 int1],'double')

How to use Bitxor for Double Numbers?

I want to use xor for my double numbers in matlab,but bitxor is only working for int numbers. Is there a function that could convert double to int in Matlab?
The functions You are looking for might be: int8(number), int16(number), uint32(number) Any of them will convert Double to an Integer, but You must pick the best one for the result You want to achieve. Remember that You cannot cast from Double to Integer without rounding the number.
If I understood You correcly, You could create a function that would simply remove the "comma" from the Double number by multiplying your starting value by 2^n and then casting it to Integer using any of the functions mentioned earlier, performing whatever you want and then returning comma to its original position by dividing the number by 2^n
Multiplying the starting value by 2^n is a hack that will decrease the rounding error.
The perfect value for n would be the number of digits after the comma if this number is relatively small.
Please also specify, why are You trying to do this? This doesn't seem to be the optimal solution.
You can just cast to an integer:
a = 1.003
int8(a)
ans =
1
That gives you an 8 bit signed integer, you can also get other size i.e. int16 or else unsigned i.e. uint8 depending on what you want to do

num2hex vs dec2hex in MATLAB

I don't understand the difference between hex2dec and hex2num and their opposites in MATLAB.
Say I had a hex value, 3FD3B502C055FE00. When I use hex2dec, I get 4.5992e+018
. When I use hex2num, I get 0.3079. What's going on?
These functions work very differently, as you noticed. hex2dec converts a hexadecimal string to a floating-point number by raw byte conversion, and I think you found that this works as you were expecting. However, hex2num converts a hexadecimal string to its IEEE double-precision representation.
The IEEE 754 double precision standard calls for a one-bit sign, a 11-bit exponent, and a 52-bit fraction. So hex2num parses the hexadecimal in this format, yielding a very different result from hex2dec.
hex2dec -
Convert hexadecimal number string to decimal number
Description
d = hex2dec('hex_value') converts hex_value to its floating-point integer representation. The argument hex_value is a hexadecimal integer stored in a MATLAB string. The value of hex_value must be smaller than hexadecimal 10,000,000,000,000.
If hex_value is a character array, each row is interpreted as a hexadecimal string.
hex2num -
Convert hexadecimal number string to double-precision number
Description
n = hex2num(S), where S is a 16 character string representing a hexadecimal number, returns the IEEEĀ® double-precision floating-point number n that it represents. Fewer than 16 characters are padded on the right with zeros. If S is a character array, each row is interpreted as a double-precision number.
NaNs, infinities and denorms are handled correctly.
Knowing that 3FD3B502C055FE00 is bigger than (10,000,000,000,000)16, out of range.

Matlab function/script to convert a real number to a hexidecimal single precision repersentation and back again [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
32 bit hex to 32 bit floating point (IEEE 754) conversion in matlab
I am trying to test the functionality of a filter written in VHDL, the input of this filter is a single precision floating point number. To do this I want to convert an array of real numbers in MATLAB, representing a sine wave to an array of hexadecimal representation of floating point numbers. Apply this array to the filter and convert the output to real values.
I.e. I need a function to perform the following -3.48 = 0x"C05EB851", the function performed on this site and it's inverse.
Does anyone have a MATLAB function/m-file to perform this operation? any help is greatly appreciated
Cheers
>> help num2hex
NUM2HEX Convert singles and doubles to IEEE hexadecimal strings.
If X is a single or double precision array with n elements,
NUM2HEX(X) is an n-by-8 or n-by-16 char array of the hexadecimal
floating point representation. The same representation is printed
with FORMAT HEX.
Let's try your example:
>> num2hex(single(-3.48))
ans =
c05eb852
close enough?