public static long parseLong(String s, int radix) throws NumberFormatException Examples:- parseLong("0", 10) returns 0L parseLong("473", 10) returns 473L parseLong("-0", 10) returns 0L parseLong("-FF", 16) returns -255L parseLong("1100110", 2) returns 102L parseLong("99", 8) throws a NumberFormatException parseLong("Hazelnut", 10) throws a NumberFormatException parseLong("Hazelnut", 36) returns 1356099454469L What is a radix, and what it does in the above code examples ?

Munish Gulati<br />SCJP 1.4<br />Albert Einstein: There are only two ways to live your life. One is as though nothing is a miracle. The other is as though everything is a miracle.

JCP Help, Welcome to JavaRanch! We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy. Thanks Pardner! Hope to see you 'round the Ranch!

Does knowing that radix means base help to better understand these examples (which were taken from the Long class documentation)? To review a couple of the examples briefly, parseLong("99", 8) throws a NumberFormatException because a base 8 numbering system was specified. In such a system, only 8 different digits would be used, presumably 0, 1, 2, 3, 4, 5, 6, and 7 in this case. Since 8 is not one of these digits, there is a problem with trying to turn it into a number. You're likely used to the decimal system with its ten digits - 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9. Realize that we don't have to count with ten digits, we just do, probably because we (usually) have ten digits attached to our hands. I would guess that the example that specified a 36 digit numbering system (base 36, a.k.a. radix 36), parseLong("Hazelnut", 36), uses 0-9 as the first ten digits, representing their "normal" decimal values, with the characters of the english alphabet representing the following 26 values - ten, eleven, twelve, thirteen... So, A is ten, B is eleven, etc. I suppose the next challenge would be to understand how to convert these different radix numbers into decimal. It's pretty darn easy. Realize that, in decimal, two hundred and fourty-six (246) is the same as (6 X 10^0) + (4 X 10^1) + (2 X 10^2) = 246 base 10 Notice all those tens and remember "decimal" means "base ten". Well, to interpret (i.e. convert to decimal) a number such as 46 base 7 isn't all that different. 46 base 7 = (6 X 7^0) + (4 X 7^1) = 6 + 28 = 34 base 10 Making any sense?

Munish Gulati
Ranch Hand

Joined: Aug 06, 2003
Posts: 37

posted

0

Thanks Dirk My doubts are clear now. Changed my name too.

Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023

posted

0

Karan, thanks for changing your display name, but we aren't quite there yet. We'd appreciate separate first and last names. Thanks.