Typecast a hex string to single

35 次查看(过去 30 天)
I have strings of hex values that I need to typecast to single precision values. I don't actually care about the actual single precision value, I am just trying to package the binary into a single datatype for transmission.
I want something like
MyHexString = '7f8e2d38';
out = typecast(MyHexString,'single'); %This doesn't work because typecast needs number
But the typecast.m function requires a numeric value. All I want to do is to produce the single precision number equivalent of the binary data held in my hex string.
FYI: I was using
out = typecast(uint32(hex2dec(MyHexString)),'single');
This worked most of the time but is occasionally produced incorrect results because the hex2dec function produces a double and there is some loss on the subsequent uint32 cast. At least that's what I think is wrong.
How to I package hex into a single datatype?
  2 个评论
James Tursa
James Tursa 2022-8-18
编辑:James Tursa 2022-8-18
"... occasionally produced incorrect results ..."
Can you post some specific examples where this happens? How can there be a loss converting to uint32?

请先登录,再进行评论。

采纳的回答

Jan
Jan 2022-8-18
编辑:Jan 2022-8-18
str = '7f8e2d38';
vec = uint8(sscanf(str, '%2x'));
num = typecast(vec, 'single')
num = single 4.1379e-05
num = typecast(flip(vec), 'single') % Maybe MSB?
num = single NaN
NaN due to initial 7f
  13 个评论
Walter Roberson
Walter Roberson 2022-8-19
You are transmitting in binary, right? Rather than formatting the single precision data as text?
Because if so then it does not matter that the printable version of a series of bytes might be NaN, as long as the byte sequence is exactly recoverable when transmitted in binary.
format long g
s = '7f8e2d38';
d = uint8(sscanf(s, '%2x').')
d = 1×4
127 142 45 56
df = typecast(fliplr(d), 'single')
df = single
NaN
typecast(df, 'uint8')
ans = 1×4
56 45 142 127
filename = tempname();
fid = fopen(filename, 'w')
fid =
3
fwrite(fid, df, 'single')
ans =
1
fclose(fid)
ans =
0
ls('-l', filename)
-rw-r--r-- 1 mluser worker 4 Aug 19 18:21 /tmp/tp3dab8982_f12e_4817_9513_f7aa0e567ad9
fid = fopen(filename, 'r')
fid =
3
dfr = fread(fid, '*single')
dfr = single
NaN
fclose(fid)
ans =
0
typecast(dfr, 'uint8')
ans = 1×4
56 45 142 127
Michael
Michael 2022-8-19
Yes. That's what I was originally doing i.e. transmitting the raw binary. I believe the main issues I was having is that I was converting the double to a hex and then splitting up that hex. When one part of the resulting split hex string would result in a NaN value, using () +1i() in matlab would then force the other part of the number to be NaN as well. I got around that by using complext(real, imag). The code I posted above that splits the digits rather than the hex seems to do what I need related to transmitting time values. I believe that the transmission of the raw binary would work as well. Thanks for all your help!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by