Skip to content
August 23, 2008 / Narayan

Converting Number System

public static void main(String[] args) {
int binary = 4;
int hexadecimal = 43;

//Convert the integer to binary. Stored in String.
String bin = Integer.toBinaryString(binary);
System.out.println(binary + ” as binary: ” + bin);

//Convert the second integer into hex. Stored in String.
String hex = Integer.toHexString(hexadecimal);
System.out.println(hexadecimal + ” as hex: ” + hex);

//To convert back, parse the String with base 2 (binary).
System.out.println(bin + ” as integer: ” + Integer.parseInt(bin, 2));

//Hexadecimal is base-16, so pass in 16 as base.
System.out.println(hex + ” as integer: ” + Integer.parseInt(hex, 16));

//Just paste these java codes in your java file and do not import any thing in java.

//Now u will be able to easily convert the Number System to each other in java


One Comment

Leave a Comment
  1. jiv / Nov 26 2012 3:04 pm

    how can i convert a signed integer to Binary, Decimal, Octal and Hexadecimal in just one code/file?.. and would loop at least ten times then generate its results after..?? thanks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: