Hex to binary character array

37 visualizaciones (últimos 30 días)
Yannick Pratte
Yannick Pratte el 11 de Jul. de 2020
Comentada: Walter Roberson el 12 de Jul. de 2020
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 comentario
Voss
Voss el 11 de Jul. de 2020
Looks like a has some double quotes in there that may be causing the problem.

Iniciar sesión para comentar.

Respuesta aceptada

Les Beckham
Les Beckham el 11 de Jul. de 2020
Editada: Les Beckham el 11 de Jul. de 2020
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 comentarios
Yannick Pratte
Yannick Pratte el 12 de Jul. de 2020
Thanks guys! That works!
Walter Roberson
Walter Roberson el 12 de Jul. de 2020
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]

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 11 de Jul. de 2020
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 comentario
madhan ravi
madhan ravi el 11 de Jul. de 2020
+1, the lookup is more intuitive ;)

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by