how to convert binary string to m ary numbers?
Afficher commentaires plus anciens
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 commentaire
Guillaume
le 1 Juil 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?
Réponses (2)
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
le 1 Juil 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,[]);
Catégories
En savoir plus sur Data Type Conversion dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!