필터 지우기
필터 지우기

Hex to binary character array

조회 수: 38 (최근 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
Voss
Voss 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
Yannick Pratte
Yannick Pratte 2020년 7월 12일
Thanks guys! That works!
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 ;)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by