How do you convert base m to base n

73 views (last 30 days)
Hello guys. I know this is stupid but can someone help me about this. Im asked to convert any number or letter from base m to base n. from bimary to deci, vice versa, binary to hexa and vice versa, and binary to octal and vice versa WITHOUT using inbuilt matlab conversiom commands. Im only limited to the REM and MOD command. Can someone help me? I really dont know what todo. Thank you
So after reading comments Im heartly to say sorry. It's my first time here and I only rushed the question.
So here I first converted my input to a string using string2num. Then I used the length function to determine length of input. Now i'm stuck at the conversion. Because it is required that the code is IN general, not in piece by piece. Like binary to 3 binary to 4 like that. I know that using modulo and then having a remainder 0 will OUTPUT 0 and remainder 1 will OUTPUT 1 If we were to convert base m to base 2. I really am stuck now for the general coding, cause iknow theres a division and multiplication process here. Sorry for taking up your precious time
  1 Comment
John D'Errico
John D'Errico on 8 Dec 2015
So start writing. Think down what it means to write a number in base N. WHAT DO THOSE DIGITS MEAN? Once you know that, you should be able to convert a digit string to base 10. (As a double precision number)
Now, how can you convert a number from base 10 (again, as a double) into base M? What, for example, does mod(x,M) tell you about the number x?
Break the problem down into small, sub-problems. This is always necessary for something that is too hard for you to solve as a whole.
Make an effort. If you are willing to give up without trying, then there is no reason for us to do your homework for you. MATLAB gurus help those who help themselves.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 23 Aug 2016
Edited: John D'Errico on 25 Aug 2016
Since this question is now far out of date, and of no value to the OP, I'll now post an answer. (I'm sorry, but I won't do your homework. For someone who says that I will have solved some future HW question, that person will have needed to search for this question, then read my response and understand it.) The problem is how to convert between numeric bases, so from general base m to base n.
The simple solution of course is to use base2dec, and then dec2base. Thus, use inbuilt tools to do the conversion, but using decimal as the intermediate. (Yes, I know that this uses existing tools and the requirement is to not do so, but it is still worth mentioning.) I'll use them anyway for an example, so I can test the later result.
Start with a binary number N2. Lets see if we can convert to a ternary number base (thus base 3.)
N2 = '1011111';
Use bin2dec to do the conversion.
D = bin2dec(N2)
D =
95
So this has a decimal value of 95. Easy enough. Now to base 3.
N3 = dec2base(D,3)
N3 =
10112
You should recognize that N2 and N3 are stored in a character form. I chose a value for N2 that should convince you this is in base 3 at least, since the units digit is a 2.
If your starting and ending bases were not 2 and 3, just substitute the desired basal values in there, wherever you see a 2 or 3.
Next, lets see how we might do the above base conversion using MATLAB itself, but without the base conversion tools. First, convert the bits of N2 into numerically stored bits. We do so by subtracting the character '0' from N2.
B = N2 - '0'
B =
1 0 1 1 1 1 1
So this is still in binary form, but I can now do arithmetic with those binary bits.
D = sum(B2.*2.^(numel(B2)-1:-1:0))
D =
95
The above computation used no more than sum. If your homework won't even let you use sum, then just use a loop. As you can see, I got lucky, and 95 is the decimal value, as we knew from before. :) Really, it was not luck.
So now we just go back into base 3. I'll just strip off each ternary (base 3) digit, one at a time. I'll use mod to do the work here, and build up the base 3 "digits" into a vector. I might have been smarter yet, and preallocated the vector N3 to be the proper size. That would have helped me by not growing the vector. And I could have determined the number of digits needed by computing the log(D) to the base 3. (Think how this would work. Just a basic log identity needed.)
N3 = [];
while D ~= 0
N3 = [mod(D,3),N3];
D = (D - N3(1))/3;
end
N3
N3 =
1 0 1 1 2
Oops. I need to convert back into the standard character form.
N3 = char(N3 + '0')
N3 =
10112
So base 2 to decimal, then back into base 3. Easy to do. This will work as long as the value represents a number that does not exceed 2^53-1. That ensures the value is exactly representable as an integer when in double precision form. I suppose I could have used uint64 as the intermediate decimal space, which would handle larger values up to 64 bits.
Beyond 64 bits in size, or to move directly between two bases, we would need to be more tricky. Still doable, but more work. We need to do all of the arithmetic ourselves. So suppose we wanted to compute the number '13023', which we will assume represents a number in base 4, into a base 3 representation? By way of reference, here is the answer we should expect, using base 10 as the intermediate representation.
base2dec('13023',4)
ans =
459
dec2base(459,3)
ans =
122000
So, what does the number 13023 mean as a base 4 number? Just write it out!
1*4^4 + 3*4^3 + 0*4^2 + 2*4^1 + 3*4^0
ans =
459
So that part did work after all. But now lets do all of the arithmetic in base 3. To start with, the number 4^0, in base 3 is simple to write. It can be thought of as
[0 0 0 0 0 1]
where each number represents the value in base 3. So, 4^1 must be 4 times that.
[0 0 0 0 0 1]*4 = [0 0 0 0 0 4]
But, remember, we need to do carries, so that this number now lives in base 3.
[0 0 0 0 0 1]*4 = [0 0 0 0 1 1]
We can think of this as the statement that
4 = 1*3^1 + 1*3^0
Then what is 4^2, written in base 3? 4^2 = 4*4^1.
[0 0 0 0 1 1]*4 = [0 0 0 0 4 4]
Do some carries, and we get
4^2 = [0 0 0 1 2 1]
in base 3. (Check it if you don't believe me!)
Likewise, we can compute the higher poswers of 4 simple in base 3 as
4^3 = [0 0 2 1 0 1]
4^4 = [1 0 0 1 1 1]
So now, lets return to the original number, 13023 in base 4.
1*4^4 + 3*4^3 + 0*4^2 + 2*4^1 + 3*4^0
1*[1 0 0 1 1 1] + 3*[0 0 2 1 0 1] + 0*[0 0 0 1 2 1] + 2*[0 0 0 0 1 1] + 3*[0 0 0 0 0 1]
ans =
1 0 6 4 3 9
But, we need to do carries on those digits. In base 3, remember that no digit can be larger then 2. So, starting from the right these numbers are equivalent, when the "digits" must live in base 3:
[1 0 6 4 3 9] =
[1 0 6 4 6 0] =
[1 0 6 6 0 0] =
[1 0 8 0 0 0] =
[1 2 2 0 0 0]
So finally, we have the base 3 representation. It is exactly what we expected, but we did all of the arithmetic in base 3, never really going into base 10.
  3 Comments
James Tursa
James Tursa on 25 Aug 2016
Can you check what you posted? I can't get it to run the example case, for one.
John D'Errico
John D'Errico on 27 Aug 2016
Edited: John D'Errico on 27 Aug 2016
Hmm. Will check...
OK, checked. Sorry about that. :) I'd added some capability after writing the code and testing it, but then flubbed an error check and did not retest it. Doh.
I've uploaded a new version.
base2base('12023',4,3)
ans =
112122

Sign in to comment.

More Answers (1)

Tarinda Tiushan
Tarinda Tiushan on 23 Aug 2016
Try dec2bin ('decimal number to covert to binary') Ex. Dec2bin ('362728') Vise versa bin2dec
  1 Comment
Walter Roberson
Walter Roberson on 23 Aug 2016
John D'Errico comments:
This fails to answer the question as posed. It shows only how to convert between base 10 and 2, and it uses the build-in tools, when they were excluded from use in the question.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!