numerical conversion methods..

i was looking for any methods in Math package that can convert any number such as [DECIMAL-OCTAL],

[BINARY-OCTAL], [HEXA-OCTAL], [BINARY-HEXA], ETC.. (my proffesor told me,) but i cant find any of it,

are there any methods such like that? or should i define my own class to perform this tasks?

Re: numerical conversion methods..

Technically, it only matters on displaying them, or in the actual code, since when the program is running, everything is in binary. To enter a hex number add the **0x** to the front of the number. Unfortunately, I don't know what to put to enter in as binary or octal, but I do know decimal has no prefix :)

In the printf function, I believe if you do "%0xd", it should print out the value as a hex, but i'm not positive.

Re: numerical conversion methods..

How about this:

Code :

System.out.println(Integer.toHexString(195012));
System.out.println(Integer.toBinaryString(195012));
System.out.println(Integer.toOctalString(195012));

Expected output would be:

Quote:

2f9c4

101111100111000100

574704

// Json

Re: numerical conversion methods..

Damn! thats it!!! that's what i've been looking for! tnx sir JSon!!!! your really awesome!

i never knew that those methods were in Integer class..I thought it was in Math. Im using netbeans but i didnt notice all of those methods when everytime im using that class

\m/ \m/ \m/ \m/ \m/ \m/ \m/ \m/ \m/ \m/ \m/ \m/ \m/

Re: numerical conversion methods..

my question is, regarding with OCTAL conversion.. why is it converted into String?

(i understand a liitle bit with hexa and binary) ..

and how could i reverse those methods?

if i input and i want it to convert into a Decimal? or

if i input and i want it to convert into a Decimal?

hmm.

Re: numerical conversion methods..

You'll have some trouble converting the second number to an integer... it's longer than 4 bytes :P

Binary String to Decimal int:

Code :

public static int binaryToDecimal(String num)
{
int sum = 0;
for (int i = 0; i <num.length(); i++)
{
sum += Integer.parseInt(""+num.charAt(i))*Math.pow(2,num.length()-1-i);
}
return sum;
}

Hex works basically the same way, except you have to figure out how to get the value of the number at each digit differently because parseInt doesn't recognize A-F as 10-15 :P

Re: numerical conversion methods..

Ok, let me extend my first example.

Code :

System.out.println("195012 hex: " + Integer.toHexString(195012));
System.out.println("195012 binary: " + Integer.toBinaryString(195012));
System.out.println("195012 octal: " + Integer.toOctalString(195012));
System.out.println("From hex: " + Integer.decode("#2f9c4"));
System.out.println("From binary: " + Integer.parseInt("101111100111000100", 2));
System.out.println("From octal: " + Integer.decode("0574704"));

Output:

Quote:

195012 hex: 2f9c4

195012 binary: 101111100111000100

195012 octal: 574704

From hex: 195012

From binary: 195012

From octal: 195012

// Json

Re: numerical conversion methods..

WOW TNX AGAIN SIR JSON!! \m/

TNX A LOT!!

Re: numerical conversion methods..

ahh so thats the purpose of the .decode() method of Integer Class... tnx again!! \m/

Re: numerical conversion methods..

but why .parseInt in binary?

Code :

System.out.println("From binary: " + Integer.parseInt("101111100111000100", 2));

Re: numerical conversion methods..

Because there does not appear to be a decode for the binary number.

// Json

Re: numerical conversion methods..

You could also have used **parseInt("12345670",8)** for octal numbers.

Re: numerical conversion methods..