#Hexadecimal to decimal program not working
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
`` public static int hexDigitToDec(String hex) {
String numbers = "0123456789ABCDEF";
int val = 0;
int length = hex.length();
if (numbers.contains(hex) && length == 1) {
val = numbers.indexOf(hex);
}
return val;
}
public static int hexToDec(String hexidecimal) {
int decValue = 0;
int pos = 0;
Character c = 'e';
String s = "";
for (int i = 0; i > hexidecimal.length(); i++) {
c = hexidecimal.charAt(i);
s = c.toString();
decValue = (int) (decValue + (hexDigitToDec(s) * Math.pow(16, pos)));
pos++;
}
return decValue;
}``
A heads up, this is part of Java already:
Integer.parseInt(hex,16); hex to dec
and
Integer.toHexString(12)
also why length == 1? And hard to tell, depends on your instructions
length==1 so that it only parses one at a time
did i overcomplicate the program?
ah, you're doing that there. And yes
also an issue I already see if you have a lowercase letter in your hextodec method, and uppercase letters in the latter
wyn
wym
Also it was a problem in the loop, but now it's giving me a result of 3025 for 1DB (should be 475)
Character c = 'e'; String numbers = "0123456789ABCDEF";
c != C
I know that
Again, it's a placeholder cause you can't initialize empty chars
Fixed it
I had it running backwards lol
which value are you passing in?
1DB