Hex to binary character array
Mostra commenti meno recenti
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 Commento
Voss
il 11 Lug 2020
Looks like a has some double quotes in there that may be causing the problem.
Risposta accettata
Più risposte (1)
Walter Roberson
il 11 Lug 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 Commento
madhan ravi
il 11 Lug 2020
+1, the lookup is more intuitive ;)
Categorie
Scopri di più su Data Type Conversion in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!