This is my method for converting a String Hex to a decimal:
private int fromHexToPercent(String hex){
int decimal = Integer.parseInt(hex, 16);
return decimal;
}
When i call it on these values: #ffe87e, #ffc6bb, #528b7a, #9b81ff, #7d6a32, #000c40 among others I get the following exception on this line:
int decimal = Integer.parseInt(hex, 16);
Error:
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:481)
Here is my method for converting from rgb to hex, i got a feeling that Java has å problem with this line: hex = “0” + hex;. But it needs to be there to get the hex-code right.
public static String toHex(int r, int g, int b) {
return "#" + toBrowserHexValue(r) + toBrowserHexValue(g) + toBrowserHexValue(b);
}
private static String toHexValue(int number) {
String hex = Integer.toHexString(number & 0xff);
while (hex.length() < 2) {
hex = "0" + hex;
}
return hex;
}
Really hope somebody can see a solution to this problem =)
The only valid hex digits are 0-9 and a-f (or A-F).
#is not a valid hex digit.