# Octal to Decimal conversion in JAVA

This tutorial would help in learning the following:

• Conversion of numbers from octal-system (0 to 7) to decimal-system (using 0 to 9)
• Re-conversion of characters into their respective ASCII codes
• Code for Octal to Decimal conversion in JAVA

The decimal number system uses all the 10 digits (0 to 9) to represent a number while the octal number system used eight digits (0 to 7). The following algorithm is used to convert an octal number to a decimal number:

let the octal number be 77

1. Give proper index to each digit of the octal number, starting with zero and from the rightmost digit
number->77
index—->10
2. multiply the number present at index ‘i’ with the ith power of  8
index 0 -> 7* 80 = 7
index 1 -> 7 * 81 = 56
3. add all the products obtained from step 2, to get the equivalent decimal number
7 + 56 = 63
(77)8 ~ (63)10

Each character used in computing is assigned a special ASCII code (decimal-number-system); hence, once an octal code is converted to its equivalent decimal-number code, the latter can be assigned a character as per the ASCII convention.
Note, there can be different encoding systems on different machines.

## Code for Octal to Decimal conversion:

```//This program follows the convention of giving a fullstop after the code for an individual character
public class OctalDeconversion
{
String octalcode;
OctalDeconversion()//default constructor
{
octalcode="103.157.144.145.40.123.160.145.145.144.171.";
}
OctalDeconversion(String x)//parameterized constructor
{
octalcode=x;
}
public static void main(String args[])
{
OctalDeconversion ob;
try{//considering the command line argument of the user as the string to be re-converted from octal format
ob=new OctalDeconversion(args[0]);
}//going with the default string in case of any exceptions with the command line argument
catch(Exception e){ob=new OctalDeconversion();};
System.out.print("The decoded value of octal code "+ob.octalcode+" is: ");
ob.octalToString(ob.octalcode);
}
char octal(char array[])
{
int code=0;
int temporary;
int i;
int j=0;
for(i=0;array[i]!='\0';i++);//obtaining the index of the last code-character in array[]
for(--i;i>=0;i--){
temporary = ((int)array[i]) - ((int)('0'));
code+=temporary*(int)(Math.pow(8,j++));//Math.pow() helps in giving the required power of 8
}
return (char)code;//returning the character corresponding to the integer-code whose value is in 'code'
}
void octalToString(String code)
{
char helper[]=new char[10+1];//assuming that the length of octal code for one character will not exceed 10
int index = 0;
for(int i=0;i<code.length();i++){
if(code.charAt(i)=='.')//printing the value of the code for one character
{
helper[index]='\0';
System.out.print(octal(helper));
index=0;
}
else//stacking up the incomplete code for one character
{
helper[index++]=code.charAt(i);
}
}
}
}//class ends```

## Output: (without command-line argument)

`The decoded value of octal code 103.157.144.145.40.123.160.145.145.144.171. is: Code Speedy`

## Summary:

char octal(char[]):

• the index of the last character in array[] is stored in ‘i’
• the value of ‘j’ corresponds with the required power of 8 that is required for the character at given index ‘i’
• the numerical value of the character-code at index ‘i’ is stored in ‘temporary’
• the sum of products for different indices is stored in ‘code’
• the character corresponding to the value of ‘code’ is returned

void octalToString(String)

• helper[] stores the octal code for one character at a time
• octal(char[]) is called and helper[] is passed to it to print the corresponding character for the octal code in helper[]