MATLAB Answers

Hex to binary character array

33 ビュー (過去 30 日間)
Yannick Pratte
Yannick Pratte 2020 年 7 月 11 日
コメント済み: Walter Roberson 2020 年 7 月 12 日
Hi,
I am trying to convert an Heaxdecimal data to a binary format. The value is extracted from a CSV files and I get a value (1 x 102 char). Each of the are comprise between 0 and f, but the function hex2dec do not interpret this type of data directly. I am unable to convert this string to binary nor decimal.
>> a = Data{1,12}{1,33}{1,1}
a =
'"8000000000200000000000000000000005e380924000012808800000c0000000000000000013f000111f0000000000000000"'
>> hex2dec(a)
Error using hex2dec>hex2decImpl (line 58)
Input to hex2dec should have just 0-9, a-f, or A-F.
Error in hex2dec (line 21)
d = hex2decImpl(h);
Would you know which way I will be able to convert this data?
Regards,
Yannick
  1 件のコメント
Benjamin
Benjamin 2020 年 7 月 11 日
Looks like a has some double quotes in there that may be causing the problem.

サインインしてコメントする。

採用された回答

Les Beckham
Les Beckham 2020 年 7 月 11 日
編集済み: Les Beckham 2020 年 7 月 11 日
This string is way too long to be converted to a decimal (or binary) number directly. It is actually 100 hex digits long (not 102 as you say in your question).
To convert this string to binary on a byte-by-byte basis (two characters per byte) you can do this:
a = '8000000000200000000000000000000005e380924000012808800000c0000000000000000013f000111f0000000000000000';
>> binresult = dec2bin(hex2dec(reshape(a, numel(a)/2, [])))
binresult =
50×8 char array
'10001000'
'00000000'
'00000000'
'00000000'
'00000000'
'00000000'
'00001100'
'00000000'
'00000000'
'00000000'
'00100000'
'00000000'
'00000000'
'00000000'
'00000000'
'00000000'
'00000000'
'00000000'
'00000000'
'00000000'
'00000000'
'00000000'
'00000000'
'00000000'
'00000001'
'00000011'
'00001111'
'00000000'
'00000000'
'00000000'
'00000001'
'00000001'
'00000001'
'01011111'
'11100000'
'00110000'
'10000000'
'00000000'
'10010000'
'00100000'
'01000000'
'00000000'
'00000000'
'00000000'
'00000000'
'00010000'
'00100000'
'10000000'
'00000000'
'10000000'
Note that this only works if the number of characters in the string is even.
For 16 bit binary results change numel(a)/2 to numel(a)/4.
Note that Benjamin's comment is correct, you can't surround a string or char vector with both single and double quotes. For this, use the single quotes to create a char vector. If your data is a string, try replacing a in the above with char(a).
  4 件のコメント
Walter Roberson
Walter Roberson 2020 年 7 月 12 日
reshape() works down columns, but dec2bin() creates rows. That's why I had the transpose after the dec2bin: make the rows of bits into columns so that the reshape() will put them adjacent the way you want
A = [11 12 13 14;
21 22 23 24];
A(:) -> [11 21 12 22 13 23 14 24].'
reshape(A',1,[]) -> [11 12 13 14 21 22 23 24]

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2020 年 7 月 11 日
a = '8000000000200000000000000000000005e380924000012808800000c0000000000000000013f000111f0000000000000000';
b = reshape(dec2bin(sscanf(a, '%1x'),4).', 1, []);
or
LUT('0':'9', :) = dec2bin(0:9, 4);
LUT('a':'f', :) = dec2bin(10:15, 4);
LUT('A':'F', :) = dec2bin(10:15, 4);
a = '8000000000200000000000000000000005e380924000012808800000c0000000000000000013f000111f0000000000000000';
b = reshape(LUT(a, :).', 1, []);
Neither of these rely upon a being an even number of digits.
The lookup table method is probably more efficient.
In both cases, the "binary" that is emitted is '0' and '1' the characters rather than 0 and 1 the decimal digits. To get the decimal digits, subtract '0' (character 0), such as
LUT('0':'9', :) = dec2bin(0:9, 4) - '0';
temp = dec2bin(10:15, 4) - '0';
LUT('a':'f') = temp;
LUT('A':'F') = temp;
  1 件のコメント
madhan ravi
madhan ravi 2020 年 7 月 11 日
+1, the lookup is more intuitive ;)

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by