Converting from decimal to hexadecimal:

Integer.toHexString(int i)

Decimal to octal

Integer.toOctalString(int i)

Decimal to binary

Integer.toBinaryString(int i)

Conversion from hexadecimal to decimal

Integer.valueOf(“FFFF”,16).toString()

Octal is converted to decimal

Integer.valueOf(“876”,8).toString()

Binary to decimal

Integer.valueOf(“0101”,2).toString()



Is there any way to convert 2,8,16 to 10 directly?

Java. Lang. Integer class

parseInt(String s, int radix)

Parses a string argument to a signed integer, using the cardinality specified by the second argument.

examples from jdk:

parseInt(“0”, 10) returns 0

parseInt(“473”, 10) returns 473

parseInt(“-0”, 10) returns 0

parseInt(“-FF”, 16) returns -255

parseInt(“1100110”, 2) returns 102

parseInt(“2147483647”, 10) returns 2147483647

parseInt(“-2147483648”, 10) returns -2147483648

parseInt(“2147483648”, 10) throws a NumberFormatException

parseInt(“99”, throws a NumberFormatException

parseInt(“Kona”, 10) throws a NumberFormatException

parseInt(“Kona”, 27) returns 411787



How to write base conversion (two, eight, sixteen) without algorithm

Integer.toBinaryString

Integer.toOctalString

Integer.toHexString





Example 2



public class Test{

public static void main(String args[]){



int i=100;

String binStr=Integer.toBinaryString(i);

String otcStr=Integer.toOctalString(i);

String hexStr=Integer.toHexString(i);

System.out.println(binStr);



}







Example 2

public class TestStringFormat {

public static void main(String[] args) {

if (args.length == 0) {

System.out.println(“usage: java TestStringFormat <a number>”);

System.exit(0);

}



Integer factor = Integer.valueOf(args[0]);



String s;



s = String.format(“%d”, factor);

System.out.println(s);

s = String.format(“%x”, factor);

System.out.println(s);

s = String.format(“%o”, factor);

System.out.println(s);

}

}







Other methods:



Integer.tohexstring (your decimal number);

For example,

String temp = Integer.toHexString(75);

The output temp is 4b







// Enter a decimal number and convert it to hexadecimal

import java.io.*;

public class toHex{



public static void main(String[]args){



int input; // Store the input data

// Create an instance of the input string

BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));

System.out.println(” Please enter an integer: “);

String x=null;

try{

x=strin.readLine();

}catch(IOException ex){

ex.printStackTrace();

}

input=Integer.parseInt(x);

System.out.println (” The number you entered is: “+input); // Outputs the number received from the keyboard



System.out.println (” Its hexadecimal is: “+ integer.tohexString (input)); // Convert base 10 to base 16 with toHexString

}

}