how to convert binary string to m ary numbers?

For example the given binary string is 100001000111001 and m=7. ie., to map the binary string to numbers from 0 to 6. Here the string is to be splited as 100 001 000 11 100 1 and the 7 ary sequence will be 4 1 0 3 4 1. The reverse program is also needed to convert 4 1 0 3 4 1 to 100001000111001.

댓글 수: 1

As mentioned in my answer, your splitting is extremely odd.
  • Why is there a section in the middle that is only two bits (the '11')
  • Why do you start splitting on the left and not on the right, at the least significant bit. According to your rule the numbers
1001 = 100 1 -> 4 1 in base 7
10001 = 100 01 -> 4 1 in base 7
100001 = 100 001 -> 4 1 in base 7
Three different numbers. same result.
  • What is the rule for choosing the number of bits to group together for other bases? the minimum number of bits to represent a single digit in that other base ?
  • What is binary 111 in base 7 according to your rule?

댓글을 달려면 로그인하십시오.

답변 (2개)

Guillaume
Guillaume 2015년 7월 1일
편집: Guillaume 2015년 7월 1일

1 개 추천

You'll have to do it in two steps. Use bin2dec or base2dec to convert from your base to decimal, and then use dec2base or dec2bin to convert to the other base.
sb2 = '100001000111001';
sb7 = dec2base(base2dec(sb2, 2), 7)
Note that I've ignored the extremely odd grouping that you've used in your example. If you want to use that grouping, you will have to reshape your string into rows of 3 column and pad the last row with 0.
Your grouping does not make sense to me though since the binary numbers 100 001 000 11 100 001, 100 001 000 11 100 01 and 100 001 000 11 100 1 would all have the same representation (and also note that you've only got two bits in the 11 group).
Andrei Bobrov
Andrei Bobrov 2015년 7월 1일

0 개 추천

I agree with Guillaume.
But my variant:
sb2 = '1000010001110010';
s = [repmat('0',1,mod(-numel(sb2),3)),sb2];
sb6 = sprintf('%d',bin2dec(reshape(s,3,[])')');
sb2 = reshape(dec2bin(sb6'-'0',3)',1,[]);

댓글 수: 2

Jeena
Jeena 2015년 7월 1일
Thanks for your response. This works for 7 ary. But what will i do if it is 8 or 9. How could i split the binary numbers in that case? How could i generalize this? Also it doesnt show the correct result for 111000010001110010. The result here is 702162. For 7 ary numbers must range from 0 to 6.
Guillaume
Guillaume 2015년 7월 1일
You'll have to tell us how you're splitting the binary numbers for some other base. As mentioned, your splitting rule is extremely unusual, so we can't really extrapolate to other bases.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

질문:

2015년 7월 1일

댓글:

2015년 7월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by