MATLAB Answers

hex2num can't recover value from the hex by num2hex

조회 수: 1(최근 30일)
david wang
david wang 19 Dec 2019
댓글: Walter Roberson 21 Dec 2019
my matlab script
ans =
ans =
why I can not recover the original value ?

  댓글 수: 0

로그인 to comment.

채택된 답변

Rik 19 Dec 2019
As the documentation explains, hex2num assumes the hex is a representation of a double, while num2hex also supports the single data type. That means that despite the names, these two functions are not the exact mirror of each other.
It looks like you will have to implement hex2num yourself if you want to have a single as an output.

  댓글 수: 1

david wang
david wang 21 Dec 2019
Rik: thanks for the answer and explanation !

로그인 to comment.

추가 답변(1개)

James Tursa
James Tursa 19 Dec 2019

  댓글 수: 2

david wang
david wang 21 Dec 2019
it works. Thanks.
Walter Roberson
Walter Roberson 21 Dec 2019
This will work for the case of single, but hex2dec() cannot substitute for hex2num() for the case where the input is representing double: for a lot of the important range, the decimal results produced by hex2num() would be outside what can be represented as double precision.
Instead of hex2dec, the form that is more generally usable is
sscanf(HEXSTRING, '%lx')
which gives back a uint64 that you can then typecast.
typecast(uint32(sscanf('be361af6', '%lx')),'single')
typecast(sscanf('400921fb54442d18', '%lx'), 'double')

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.




Translated by