convert binary string into hex
조회 수: 38 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Walter Roberson
2020년 12월 3일
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').');
댓글 수: 0
추가 답변 (1개)
Stephen23
2020년 12월 3일
str = '0101010011101010';
fun = @(s)dec2hex(bin2dec(s));
out = regexprep(str,'.{1,4}(?=(.{4})*$)','${fun($&)}')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Time Series에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!