# Converting Number to Base 8

• 02-16-2012, 08:24 PM
nighteagle2
Converting Number to Base 8
Hey, so now im having trouble with the second part of my assignment, converting a number to Base 8. I already wrote a program that converts the number to binary.

My Professors asking me to produce a string of the Base 8 equivalent of the original number and assign it to the instance variable of octal number.

I can't figure out how to convert a number into an Octal without doing
Code:

`String octalStr = Integer.toOctalString(numVal)`
My teacher wants us to actually convert it.

Also how do i assign the original number to the instance variable of the octal number

Thanks
• 02-16-2012, 09:57 PM
KevinWorkman
Re: Converting Number to Base 8
How would you do this on paper, without a computer?
• 02-16-2012, 10:34 PM
Norm
Re: Converting Number to Base 8
Quote:

wrote a program that converts the number to binary.
Generalize that program by changing the 2 to a variable and then call it with an 8 to get an octal String.
• 02-17-2012, 03:12 AM
nighteagle2
Re: Converting Number to Base 8
Aite thanks man. One last question. So i have this code

Code:

```public boolean checkOctal(String octalStr, int numVal)         {                                 int p = 1;                 int octalCheck = 0;                                 for(int a = 64; a>=1 && p <= 3; a /= 8)                 {                                if (octalStr.charAt(p) > 0)                         {                                 octalCheck += (a * (octalStr.charAt(p)));                         }                         p++;                        }                 if (octalCheck == numVal)                 {                         return true;                 }                 else                 {                         return false;                 }```
i can't figure out how to get this line to work
octalCheck += (a * (octalStr.charAt(p)));

When i try converting it to an int, It doesn't equal the value at the position on the string. But if i leave it as it is, it doesn't add up correctly

Thanks
• 02-17-2012, 03:19 AM
Norm
Re: Converting Number to Base 8
Quote:

i can't figure out how to get this line to work
octalCheck += (a * (octalStr.charAt(p)));
Please explain what that line is supposed to do?

To see what the code is doing, break it up into simple, single statements and print out the results after each statement.

Quote:

When i try converting it to an int, It doesn't equal the value at the position on the string. But if i leave it as it is, it doesn't add up correctly
• 02-17-2012, 03:21 AM
nighteagle2
Re: Converting Number to Base 8
ok so octalStr is the base8 number, limited to 4 digits. I want it to multiple the second number by 64, third by 8, and fourth by 1 and add it all up.
• 02-17-2012, 03:23 AM
Norm
Re: Converting Number to Base 8
What is printed out when you separate the statement into parts and print out everything for each statement?
• 02-17-2012, 03:32 AM
nighteagle2
Re: Converting Number to Base 8
when i print (octalStr.charAt(p)), it'll show the right number.

For example the base 8 of 34 is 0042. When i print
Code:

``` (octalStr.charAt(1))```
it'll print 0. which is correct
But when i do:
Code:

` octalCheck += (a * octalStr.charAt(1) );`
it'll print 3072
• 02-17-2012, 03:33 AM
nighteagle2
Re: Converting Number to Base 8
a is 64 btw sorry
• 02-17-2012, 03:38 AM
Norm
Re: Converting Number to Base 8