There are pre-translated numbers from hexadecimal to decimal. For example, 0x01, 0x10 and so on. I need to extract from it everything that is after x, so that I get a number that could fit as an index to the array.
The difficulty lies in the fact that the hex value is an Integer (NOT AS a String), that is, it is in this format:
int hexValue = 16; // 0x10
And I need to get the answer 10.
Why is this necessary? I need the hexadecimal value to be a full-fledged index for the array
Hex, binary, octal, etc are all visual presentations of a number in a specific base. Any
intis already in "binary." It could be +5 (1) and ground (0) if you want to go that deep into it.So converting an
intto another base does not make sense unless it is for display purposes.Or given a string in a particular base and converting that to an
intso it can be used in mathematical expressions or data structures is also an option.Per your comment I still believe you are somewhat confused about numeric representation. But here is how to convert a
hex stringto anint. This does not handle signed values.prints
And let me repeat that it does not make sense to try and convert any
intfrom one base to anintin another base, as int's are not really in any base at all.intsare treated asdecimalby the compiler and we talk about them as though they arebinarybecause it provides a common and accepted way to describe them.Updated
You may want to check out Integer.decode. If you are reading in encoded strings with prefixes of
x,X, and #for hex ,0for octal, and a non-zero digit for decimal, you can do the following.prints