how to convert binary string to m ary numbers?

4 views (last 30 days)
Jeena
Jeena on 1 Jul 2015
Commented: Guillaume on 1 Jul 2015
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 Comment
Guillaume
Guillaume on 1 Jul 2015
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?

Sign in to comment.

Answers (2)

Guillaume
Guillaume on 1 Jul 2015
Edited: Guillaume on 1 Jul 2015
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 on 1 Jul 2015
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 Comments
Jeena
Jeena on 1 Jul 2015
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 on 1 Jul 2015
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.

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!