Skip to content

Instantly share code, notes, and snippets.

@rjlutz rjlutz/decToBase method
Last active May 22, 2018

Embed
What would you like to do?
Assets for Grizzly Gray (Grizzly Grayscale Picker)
private String decToBase(int d, int base) {
// credit to and adapted from:
// https://stackoverflow.com/questions/13465098/decimal-to-hexadecimal-converter-in-java
// Note that there are several one-liners that can be used too
String digits = "0123456789ABCDEF";
String hex = "";
if (d <= 0) hex = "0";
while (d > 0) {
int digit = d % base; // rightmost digit
hex = digits.charAt(digit) + hex; // string concatenation
d = d / base;
}
hex = (hex.length()<2) ? "0" + hex : hex;
return hex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.