# Interger.parseInt(String str, int radix), Long.parseLong(String str, int radix)

Sadashiv Borkar

Ranch Hand

Posts: 49

posted 15 years ago

Hi All,

If you see the wrapper classes you will find a method called parseXX(String str, int radix)

where XX is the name of the wrapper class

Now when I read the JLS I found out the following

Byte.parseByte("A",27) returns 10 //Not in JLS

Short.parseShort("A" 27) returns 10 //Not in JLS

parseInt("Kona", 27) returns 411787

parseLong("Hazelnut", 36) returns 1356099454469L

Cna anyone please clear my doubt that what is radix 27,36...As far as i know there r only following radix(10, 16, 8 ) what's the use of this radix and how does it converts the String into an integer.Or to be specific how does it convert a String (like "Kona" and "Haze1nut") to a number(whats the methodology used?)

If you see the wrapper classes you will find a method called parseXX(String str, int radix)

where XX is the name of the wrapper class

Now when I read the JLS I found out the following

Byte.parseByte("A",27) returns 10 //Not in JLS

Short.parseShort("A" 27) returns 10 //Not in JLS

parseInt("Kona", 27) returns 411787

parseLong("Hazelnut", 36) returns 1356099454469L

Cna anyone please clear my doubt that what is radix 27,36...As far as i know there r only following radix(10, 16, 8 ) what's the use of this radix and how does it converts the String into an integer.Or to be specific how does it convert a String (like "Kona" and "Haze1nut") to a number(whats the methodology used?)

Cindy Glass

"The Hood"

Sheriff

Sheriff

Posts: 8521

posted 15 years ago

From: http://www.utm.edu/research/primes/glossary/Radix.html

We usually write numbers using a place-value system. For example, in decimal 1101 means

1.103 + 1.102 + 0.10 + 1

and in binary 1101 would mean

1.23 + 1.22 + 0.2 + 1 (= 13 in decimal).

In general, a position numbering system encodes the numbers as

anbn + an-1bn-1 + . . . + a2b2+ a1b+ a0 (0 < ai < b, i=0,1,2,...,n)

where the integer b>1 is the radix or base. So decimal is "radix 10" and binary is "radix 2." Other common systems include octal (radix 8)

and hexadecimal (radix 16, here we need to add some more digits: 'a'=10, 'b'=11, 'c'=12, 'd'=13, 'e'=14 and 'f'=15). If it is not clear for the

context which radix we are using, we usually add it as a subscript. For example, (23)4 = (21)5 = (11)10 = (b)16.

We can extend this to cover all reals if we allow the inclusion of a negative sign and a radix-point (decimal point):

(anan-1. . . a2a1a0. a-1a-2a-3 . . . )b = anbn + an-1bn-1 + . . . + a2b2 + a1b + a0 + a-1b- 1 + a-2b-2 + a-3b-3 + . . .

We usually write numbers using a place-value system. For example, in decimal 1101 means

1.103 + 1.102 + 0.10 + 1

and in binary 1101 would mean

1.23 + 1.22 + 0.2 + 1 (= 13 in decimal).

In general, a position numbering system encodes the numbers as

anbn + an-1bn-1 + . . . + a2b2+ a1b+ a0 (0 < ai < b, i=0,1,2,...,n)

where the integer b>1 is the radix or base. So decimal is "radix 10" and binary is "radix 2." Other common systems include octal (radix 8)

and hexadecimal (radix 16, here we need to add some more digits: 'a'=10, 'b'=11, 'c'=12, 'd'=13, 'e'=14 and 'f'=15). If it is not clear for the

context which radix we are using, we usually add it as a subscript. For example, (23)4 = (21)5 = (11)10 = (b)16.

We can extend this to cover all reals if we allow the inclusion of a negative sign and a radix-point (decimal point):

(anan-1. . . a2a1a0. a-1a-2a-3 . . . )b = anbn + an-1bn-1 + . . . + a2b2 + a1b + a0 + a-1b- 1 + a-2b-2 + a-3b-3 + . . .

"JavaRanch, where the deer and the Certified play" - David O'Meara

Cindy Glass

"The Hood"

Sheriff

Sheriff

Posts: 8521

Sadashiv Borkar

Ranch Hand

Posts: 49

posted 15 years ago

Hi Cindy,

Thanx for the explanation and the link it was really useful for me.But I think u misunderstood my question.What I donot understand that how does it convert the Strings like "KONA" and "Haze1Nut" to integer value.If you see carefully that the above values are not numbers.So does it uses the ascii value of each character for converting it into an appropriate number or what does it use for that.

Now if you see below that the following function gives you correct results.

parseInt("Kona", 27) returns 411787

parseLong("Hazelnut", 36) returns 1356099454469L

Now if I change the following radix to 10 It gives me a NumberFormatException

parseInt("Kona", 10) returns 411787

parseLong("Hazelnut", 10) returns 1356099454469L

So whats the reason For giving the number format exception.

I understand that

radiix of 10 is used for conversion odf decimal numbers .

radiix of 8 is used for the conversion of Octal numbers.

radiix of 16 is used for the conversion of hexadecimal numbers.

Then can anyone clarify what is the use of the radiix 27 and 36

for what type of numbers are they used.Iam confused.

Thanx in advance.

Sada

Thanx for the explanation and the link it was really useful for me.But I think u misunderstood my question.What I donot understand that how does it convert the Strings like "KONA" and "Haze1Nut" to integer value.If you see carefully that the above values are not numbers.So does it uses the ascii value of each character for converting it into an appropriate number or what does it use for that.

Now if you see below that the following function gives you correct results.

parseInt("Kona", 27) returns 411787

parseLong("Hazelnut", 36) returns 1356099454469L

Now if I change the following radix to 10 It gives me a NumberFormatException

parseInt("Kona", 10) returns 411787

parseLong("Hazelnut", 10) returns 1356099454469L

So whats the reason For giving the number format exception.

I understand that

radiix of 10 is used for conversion odf decimal numbers .

radiix of 8 is used for the conversion of Octal numbers.

radiix of 16 is used for the conversion of hexadecimal numbers.

Then can anyone clarify what is the use of the radiix 27 and 36

for what type of numbers are they used.Iam confused.

Thanx in advance.

Sada

Kourosh Keshavarzi

Ranch Hand

Posts: 66

posted 15 years ago

Hi Sadashiv,

radix 10,8,and 16 are not the only radix you can have

they are simply the more popular ones and the one

you'll need most of the time.

But you can have any radix you want ..,3,4,5,6,7,8,..100,...etc.

If you were right a Math program like calculating pi may be you might find a use for it.

Now as for why is that that when you change the following radix to 10 It gives you a NumberFormatException:

When you have radix 2 you can only use values 0 and 1

When you have radix 8 you can only use values 0 to 7

When you have radix 16 you can only use values 0 to F (15)

When you have radix 27 you can only use values 0 to q (26)

So when you change the radix to 10 "k", "o", "n" are not

allowed. Its like using 5 with binary data. You can't.

As for how does it convert the Strings like "KONA".

parseInt("Kona", 27) returns 411787

k=20, o=24, n=23, a=10

I'll use ^ to mean to the power of , not xor

So: (20*(27^3)) + (24*(27^2)) + (23*(27^1)) + (10*(27^0)) =

393660 + 17496 + 621 + 10 = 411787

Hope this helped

Best of luck

Kourosh

radix 10,8,and 16 are not the only radix you can have

they are simply the more popular ones and the one

you'll need most of the time.

But you can have any radix you want ..,3,4,5,6,7,8,..100,...etc.

If you were right a Math program like calculating pi may be you might find a use for it.

Now as for why is that that when you change the following radix to 10 It gives you a NumberFormatException:

When you have radix 2 you can only use values 0 and 1

When you have radix 8 you can only use values 0 to 7

When you have radix 16 you can only use values 0 to F (15)

When you have radix 27 you can only use values 0 to q (26)

So when you change the radix to 10 "k", "o", "n" are not

allowed. Its like using 5 with binary data. You can't.

As for how does it convert the Strings like "KONA".

parseInt("Kona", 27) returns 411787

k=20, o=24, n=23, a=10

I'll use ^ to mean to the power of , not xor

So: (20*(27^3)) + (24*(27^2)) + (23*(27^1)) + (10*(27^0)) =

393660 + 17496 + 621 + 10 = 411787

Hope this helped

Best of luck

Kourosh