MATLAB: Hex to binary character array

hex char

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

Best Answer

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).