MATLAB: Convert binary string into hex

binary to hex numbers

i need to convert this binary string [ '0101010011101010'] into hex or decimal
but in 2^4 mean (i need to convert 4bit by 4bit) not all number

Best Answer

lookup = containers.Map(cellstr(dec2bin(0:15,4)),cellstr(dec2hex(0:15)));
binary_string = '0101010011101010';
output = cellfun(@(S) lookup(S), cellstr(reshape(binary_string, 4, []).')).';
This code assumes that binary_string is a multiple of 4 bits.
This is definitely not the only way to do the task. It has the advantage, though, that it does not need to convert the inputs into numeric form.
Another way:
output = cell2mat(regexprep(cellstr(reshape(binary_string,4,[]).'), cellstr(dec2bin(0:15,4)), cellstr(dec2hex(0:15)),'once').');