Zero padded hexadecimal numbers in Java

A quick recipe, converting an int or long like 65 to a string like 000041. It’s useful for converting integer RGB colors to the common #FFBBCC kind of web encoded colors.

 

int number = 65;
String hexString = String.format("%06X", number);
System.out.println(hexString);

:>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.