num2hex vs dec2hex in MATLAB - 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.

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)))

Perl: Decimal to 32bits hex convert

I want to convert decimal number 64 into hex representation: 0x00000040. I am using
printf("0x%X", 64);
but it gives output: 0x40. Can anyone please help me how to represent the decimal number in 0x00000000 format?
You can specify the length of the field between the % and the X (e.g. %8X). By default, the number will be padded with spaces, but using a leading zero for the length (e.g. %08X) will cause printf to pad with zeroes instead. Therefore, the following can be used:
printf("0x%08X", 64);

Matlab convert form hex to float

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

how to remove last zero from number in matlab

If I set a variable in Matlab, say var1 = 2.111, after running the script, Matlab returns var1 = 2.1110. I want Matlab to return the original number, with no trailing zero. Anyone know how to do this. Thanks in advance.
By default Matlab displays results in Short fixed decimal format, with 4 digits after the decimal point.
You can change that to various other format such as:
long
Long fixed decimal format, with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.
3.141592653589793
shortE
Short scientific notation, with 4 digits after the decimal point.
Integer-valued floating-point numbers with a maximum of 9 digits do not display in scientific notation.
3.1416e+00
longE
Long scientific notation, with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.
Integer-valued floating-point numbers with a maximum of 9 digits do not display in scientific notation.
3.141592653589793e+00
shortG
The more compact of short fixed decimal or scientific notation, with 5 digits.
3.1416
longG
The more compact of long fixed decimal or scientific notation, with 15 digits for double values, and 7 digits for single values.
3.14159265358979
shortEng
Short engineering notation, with 4 digits after the decimal point, and an exponent that is a multiple of 3.
3.1416e+000
longEng
Long engineering notation, with 15 significant digits, and an exponent that is a multiple of 3.
3.14159265358979e+000
However I don't think other options are available. If you absolutely want to remove those zeros you would have to cast you result in a string and remove the trailing 0 characters and then display your result as a string and not a number.

Float to text behavior of MATLAB's fprintf()

When using fprintf to convert floats to text in a decimal representation, the output is a series of decimal digits (potentially beginning with 0).
How does this representation work?
>>fprintf('%tu\n',pi)
>>1078530011
>>fprintf('%bu\n',pi)
>>04614256656552045848
Apologies if this is very trivial; I can't find an answer elsewhere, in part because searches are swamped by the various decimal data types available.
Note that the %t and %b flags are two of the differences from C's fprintf(). According to the documentation, it prints a float or double respectively "rather than an unsigned integer." o, x and u switches between octal, hex and decimal.
This representation is the binary IEEE 754 floating point representation of the number, printed as an unsigned integer.
The IEEE 754 Converter website tells us that the IEEE 754 single-precision representation of Pi (approximately 3.1415927) is 40490FDB hexadecimal, which is 1078530011 decimal (the number that you saw printed). The '%bu' format specifier works similarly but outputs the double-precision representation.
The purpose of these format specifiers is to allow you to store a bit-exact representation of a floating-point value to a text file. The alternative approach of printing the floating-point value in human-readable form requires a lot of care if you want to guarantee bit-exact storage, and there might be some edge cases (denormalized values...?) that you won't be able to store precisely at all.
If you were to print the number as hexadecimals:
>> fprintf('%bx\n', pi)
400921fb54442d18
>> fprintf('%tx\n', single(pi))
40490fdb
then the formatters '%bx' and '%tx' are simply equivalent to using NUM2HEX:
>> num2hex( pi )
400921fb54442d18
>> num2hex( single(pi) )
40490fdb
Another way is to simply set the default output format to hexadecimals using:
>> format hex
>> pi
400921fb54442d18
>> single(pi)
40490fdb
On a related note, there was a recent article by #Loren:
"How Many Digits to Write?"
where they try to find how many decimal digits you need to write in order to retain the number's full precision when re-read in MATLAB.