0% found this document useful (0 votes)
8 views4 pages

Number Conversion

The document defines methods for converting between decimal, binary, octal, and hexadecimal number systems. It contains methods that take a number in one base and return it in another base by first converting it to decimal.

Uploaded by

Keshav Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Number Conversion

The document defines methods for converting between decimal, binary, octal, and hexadecimal number systems. It contains methods that take a number in one base and return it in another base by first converting it to decimal.

Uploaded by

Keshav Bansal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

public class numberconversion

{
public static String deciBin(long decimal) {
String binary = "";
while (decimal > 0) {
binary = (decimal % 2) + binary;
decimal = decimal / 2;
}
return binary;
}

public static String deciBin(double decimal) {


long l = (long) decimal;
return deciBin(l);
}

public static long deciOct(long decimal) {


long octal = 0, i = 1;
while (decimal > 0) {
octal = octal + (decimal % 8) * i;
decimal = decimal / 8;
i = i * 10;
}
return octal;
}

public static String deciHexa(long decimal) {


String hexa = "";
while (decimal > 0) {
long temp = decimal % 16;
if (temp < 10) {
hexa = temp + hexa;
} else {
hexa = (char) (temp + 55) + hexa;
}
decimal = decimal / 16;
}
return hexa;
}

public static long binDeci(String binary) {


long decimal = 0, i = 0;
for (int j = binary.length() - 1; j >= 0; j--) {
decimal = decimal + (binary.charAt(j) - '0') * (long) Math.pow(2, i);
i++;
}
return decimal;
}

public static long binOct(String binary) {


long decimal = 0, i = 0;
for (int j = binary.length() - 1; j >= 0; j--) {
decimal = decimal + (binary.charAt(j) - '0') * (long) Math.pow(2, i);
i++;
}
long octal = 0, k = 1;
while (decimal > 0) {
octal = octal + (decimal % 8) * k;
decimal = decimal / 8;
k = k * 10;
}
return octal;
}

public static String binHexa(String binary) {


long decimal = 0, i = 0;
for (int j = binary.length() - 1; j >= 0; j--) {
decimal = decimal + (binary.charAt(j) - '0') * (long) Math.pow(2, i);
i++;
}
String hexa = "";
while (decimal > 0) {
long temp = decimal % 16;
if (temp < 10) {
hexa = temp + hexa;
} else {
hexa = (char) (temp + 55) + hexa;
}
decimal = decimal / 16;
}
return hexa;
}

public static long octDeci(long octal) {


long decimal = 0, i = 0;
while (octal > 0) {
decimal = decimal + (octal % 10) * (long) Math.pow(8, i);
octal = octal / 10;
i++;
}
return decimal;
}
public static String octBin(long octal) {
long decimal = 0, i = 0;
while (octal > 0) {
decimal = decimal + (octal % 10) * (long) Math.pow(8, i);
octal = octal / 10;
i++;
}
String binary = "";
while (decimal > 0) {
binary = (decimal % 2) + binary;
decimal = decimal / 2;
}
return binary;
}

public static String octHexa(long octal) {


long decimal = 0, i = 0;
while (octal > 0) {
decimal = decimal + (octal % 10) * (long) Math.pow(8, i);
octal = octal / 10;
i++;
}
String hexa = "";
while (decimal > 0) {
long temp = decimal % 16;
if (temp < 10) {
hexa = temp + hexa;
} else {
hexa = (char) (temp + 55) + hexa;
}
decimal = decimal / 16;
}
return hexa;
}

public static String hexaBin(String hexa)


{
long decimal = 0, i = 0;
for (int j = hexa.length() - 1; j >= 0; j--) {
if (hexa.charAt(j) >= '0' && hexa.charAt(j) <= '9') {
decimal = decimal + (hexa.charAt(j) - '0') * (long) Math.pow(16, i);
} else {
decimal = decimal + (hexa.charAt(j) - 55) * (long) Math.pow(16, i);
}
i++;
}
String binary = "";
while (decimal > 0) {
binary = (decimal % 2) + binary;
decimal = decimal / 2;
}
return binary;
}

public static long hexaDeci(String hexa)


{
long decimal = 0, i = 0;
for (int j = hexa.length() - 1; j >= 0; j--) {
if (hexa.charAt(j) >= '0' && hexa.charAt(j) <= '9') {
decimal = decimal + (hexa.charAt(j) - '0') * (long) Math.pow(16, i);
} else {
decimal = decimal + (hexa.charAt(j) - 55) * (long) Math.pow(16, i);
}
i++;
}
return decimal;
}

public static long hexaOct(String hexa) {


long decimal = 0, i = 0;
for (int j = hexa.length() - 1; j >= 0; j--) {
if (hexa.charAt(j) >= '0' && hexa.charAt(j) <= '9') {
decimal = decimal + (hexa.charAt(j) - '0') * (long) Math.pow(16, i);
} else {
decimal = decimal + (hexa.charAt(j) - 55) * (long) Math.pow(16, i);
}
i++;
}
long octal = 0, k = 1;
while (decimal > 0) {
octal = octal + (decimal % 8) * k;
decimal = decimal / 8;
k = k * 10;
}
return octal;
}
}

You might also like