When you perform the ParseInt operation with the radix, the 11 base 16 is parsed as 17, which is a simple value. It is then printed as radix 10.
You want:
System.out.println(Integer.toString(11, 16));
This takes the decimal value 11(not having a base at the moment, like having "eleven" watermelons(one more than the number of fingers a person has)) and prints it with radix 16, resulting in B.
When we take an int value it's stored as base 2 within the computer's physical memory (in nearly all cases) but this is irrelevant since the parse and tostring conversions work with an arbitrary radix (10 by default).
When you perform the ParseInt operation with the radix, the 11 base 16 is parsed as 17, which is a simple value. It is then printed as radix 10.
You want:
System.out.println(Integer.toString(11, 16));
This takes the decimal value 11(not having a base at the moment, like having "eleven" watermelons(one more than the number of fingers a person has)) and prints it with radix 16, resulting in B.
When we take an int value it's stored as base 2 within the computer's physical memory (in nearly all cases) but this is irrelevant since the parse and tostring conversions work with an arbitrary radix (10 by default).
It's actually taking 11 in hex and converting it to decimal. So for example if you had the same code but with "A" in the string, it would output 10.
Videos
You're right that the number denotes the base. You are not right that
Integer.parseInt("52", 10)will give you 52 in base 10.
This will in fact parse the number 52 as if it were in base 10. 52 could be base 6 for all we know.
For example:
Integer.parseInt("101", 2);
Is saying parse the binary (base 2) number one-zero-one. It is not saying parse one hundred and one into binary. This will return the base 10 number 5.
Any number which is returned is a standard integer which if you try to print it will be in base 10.
Worth noting that if you try and parse a number which can not possibly exist in that base, for example:
Integer.parseInt("599", 2);
then you will get an exception.
The string you are parsing could be in a specific base which you supply with the radix command to parse to an int. The int is a numerical value and doesn't have a specific base per se.
Examples
Integer.parseInt("52", 10) returns 52
Integer.parseInt("52", 8) returns the numerical value 42 (8*5+2)
Integer.parseInt("A2", 16) returns the base 10 value 162
Ok, I think there is no built in solution for this question, but it's quite easy to solve it using about the same code I've written in the question:
public static int parseInt(final String input,final int radix)
{
int output=0;
for(int i=0;i<input.length();++i)
{
output*=radix;
final char c=input.charAt(i);
if(c>='a')
output+=c-'a'+10;
else output+=c-'0';
}
return output;
}
The problem is that java does not support unsigned integers, since "ff00ff00" is larger than a positive int could be, java counts it as out of range. A simple solution is to use Long.parseLong(string, radix), ideally you would change the type of the affected variables to longs, but you could make it work with some tricky casting too.