I'm trying to convert a number from an integer into an another integer which, if printed in hex, would look the same as the original integer.
For example:
Convert 20 to 32 (which is 0x20)
Convert 54 to 84 (which is 0x54)
An integer can be converted to a hexadecimal by using the string. ToString() extension method.
hex() function is one of the built-in functions in Python3, which is used to convert an integer number into it's corresponding hexadecimal form. Syntax : hex(x) Parameters : x - an integer number (int object) Returns : Returns hexadecimal string.
The easiest way is to use Integer.toHexString(int)
public static int convert(int n) { return Integer.valueOf(String.valueOf(n), 16); } public static void main(String[] args) { System.out.println(convert(20)); // 32 System.out.println(convert(54)); // 84 }
That is, treat the original number as if it was in hexadecimal, and then convert to decimal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With