Snippet IT IT News, Programming, Internet and Blogging

18Aug/092

Java: Format Long Integer Into Hexadecimal String

In software programming, sometime you may want to convert a long integer or integer into hexadecimal string for viewing purpose or also as a text format to be stored in text file or database.

The Java API itself does not have a class or function to convert a long into a fixed 16 characters hexadecimal string or an integer into  fixed 8 characters hexadecimal string.

The Java's Long.toHexString(long i) or Integer.toHexString(int i) only able to convert the integer value to a string of ASCII digits in hexadecimal (base 16) with no extra leading 0s and it does not support the reverse way (from hexadecimal string to long integer value).