Hexadecimal Data Convert to signed int32

26 Ansichten (letzte 30 Tage)
Rasit
Rasit am 21 Nov. 2023
Kommentiert: Les Beckham am 22 Nov. 2023
Hello everyone!
I'm try to convert from hexadecimal data to signed int32. Here is my matlab code:
----------------------------------------------------------
Gyro_const = 1.08507e-6;
rawdata = [
"0a" "31" "00" "00"
"14" "cf" "ff" "ff"
"8f" "13" "00" "00"
"36" "ce" "ff" "ff"
"cf" "1a" "00" "00"
"83" "f3" "ff" "ff"
"9e" "fc" "ff" "ff"
"82" "eb" "ff" "ff"
"17" "1b" "00" "00"
"c9" "da" "ff" "ff"
"65" "1e" "00" "00"
"14" "dc" "ff" "ff"
"e8" "29" "00" "00"
"c9" "cb" "ff" "ff"
"29" "1b" "00" "00"
"32" "ef" "ff" "ff"
"80" "f8" "ff" "ff"
"b0" "ec" "ff" "ff"
"29" "23" "00" "00"
"09" "cf" "ff" "ff"];
%for i = 1:size(rawdata,1)
for i = 1:2
data = rawdata(i,:);
fprintf("non Inverted Data\n")
[data(1),data(2),data(3),data(4)]
fprintf("Inverted Data\n")
[data(4),data(3),data(2),data(1)]
double(int32(hex2dec(strcat(data(4),data(3),data(2),data(1)))))*Gyro_const
end
----------------------------------------------------------
I run this code in 2 steps and output like this:
ans =
1×4 string array
"0a" "31" "00" "00"
Inverted Data
ans =
1×4 string array
"00" "00" "31" "0a"
ans =
0.0136
-------------------
non Inverted Data
ans =
1×4 string array
"14" "cf" "ff" "ff"
Inverted Data
ans =
1×4 string array
"ff" "ff" "cf" "14"
ans =
2.3302e+03
Of these two values "0.0136 ","2.3302e+03" comes from "0000310a", "ffffcf14" hex values.
These values convert via hex2dec() built-in function.
I manually check hex value to decimal conversion but there are problem for me, this problem caused by hex2dec() function: For example:
hex2dec(strcat("00","00","31","0a")) -> 12554 This is true value for me. But the second iteration conversion is
hex2dec(strcat("ff","ff","cf","14")) -> 4.2950e+09 This is wrong value for me.
I think this problem based on the hex2dec int32 conversion. So my question how can I the hex2dec function output:
I mean, I know that this conversion hex2dec(strcat("ff","ff","cf","14")) actually -> -12524 value.
But my result is 4.2950e+09.
My question:
How can I set hex2dec function output, signed int32? or is there a any different solution for my problem.
Kind Regards.

Antworten (1)

Les Beckham
Les Beckham am 21 Nov. 2023
Bearbeitet: Les Beckham am 21 Nov. 2023
The hex2dec function assumes unsigned integer unless you tell it otherwise. You can either typecast the unsigned result to signed or use a suffix (e.g. 's32') on the string or char vector that you pass to hex2dec. I've shown both approaches below. Note that using the suffix only works on R2020a or higher.
Gyro_const = 1.08507e-6;
rawdata = [
"0a" "31" "00" "00"
"14" "cf" "ff" "ff"
"8f" "13" "00" "00"
"36" "ce" "ff" "ff"
"cf" "1a" "00" "00"
"83" "f3" "ff" "ff"
"9e" "fc" "ff" "ff"
"82" "eb" "ff" "ff"
"17" "1b" "00" "00"
"c9" "da" "ff" "ff"
"65" "1e" "00" "00"
"14" "dc" "ff" "ff"
"e8" "29" "00" "00"
"c9" "cb" "ff" "ff"
"29" "1b" "00" "00"
"32" "ef" "ff" "ff"
"80" "f8" "ff" "ff"
"b0" "ec" "ff" "ff"
"29" "23" "00" "00"
"09" "cf" "ff" "ff"];
%for i = 1:size(rawdata,1)
for i = 1:2
data = rawdata(i,:);
fprintf("non Inverted Data\n")
[data(1),data(2),data(3),data(4)]
fprintf("Inverted Data\n")
[data(4),data(3),data(2),data(1)]
% double(int32(hex2dec(strcat(data(4),data(3),data(2),data(1)))))*Gyro_const
double(typecast(uint32(hex2dec(strcat(data(4),data(3),data(2),data(1)))), 'int32')) * Gyro_const
double(hex2dec(sprintf('0x%s%s%s%ss32', data(4:-1:1)))) * Gyro_const
end
non Inverted Data
ans = 1×4 string array
"0a" "31" "00" "00"
Inverted Data
ans = 1×4 string array
"00" "00" "31" "0a"
ans = 0.0136
ans = 0.0136
non Inverted Data
ans = 1×4 string array
"14" "cf" "ff" "ff"
Inverted Data
ans = 1×4 string array
"ff" "ff" "cf" "14"
ans = -0.0136
ans = -0.0136
  3 Kommentare
Rasit
Rasit am 22 Nov. 2023
Really Thnx!
Les Beckham
Les Beckham am 22 Nov. 2023
You are quite welcome.
If this resolves your issue, please click "Accept this answer". Thanks.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Search Path finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by