Google Interview Question
SDE1sCountry: United States
This is an simple algorithm of how to convert Hex to Decimal and how to build RGB color, for example #4d4d4d = rgb(77, 77, 77).
public String convertColorHexToDecimal(String hex) {
StringBuilder result = new StringBuilder();
int counterDivider = 0;
StringBuilder buf = new StringBuilder();
for (int i = 0; i < hex.length(); i++) {
counterDivider ++;
buf.append(hex.charAt(i));
if (counterDivider == 2) {
result.append(convertHexToDecimal(buf.toString())).append(" ");
buf.delete(0, buf.length());
counterDivider = 0;
}
}
return result.toString();
}
public int convertHexToDecimal(String hex) {
int result = 0;
for (int i = 0; i < hex.length(); i++) {
result += Character.getNumericValue(hex.charAt(hex.length() - 1 - i)) * Math.pow(16, i);
}
return result;
}
I did not understand please clarify
- Shrishty February 23, 2018