#Hexadecimal to decimal program not working

1 messages · Page 1 of 1 (latest)

small steppe
#

I'm working on a program to convert hex to decimal for some practice, but I can't figure out why it's not working. The program runs, but throws a 0 regardless of input.

maiden krakenBOT
#

<@&987246399047479336> please have a look, thanks.

small steppe
#

`` 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;
}``

fierce glade
#

A heads up, this is part of Java already:
Integer.parseInt(hex,16); hex to dec
and
Integer.toHexString(12)

small steppe
#

Ah

#

It's for an assignment and I think he wants us to do it manually?

fierce glade
#

also why length == 1? And hard to tell, depends on your instructions

small steppe
#

did i overcomplicate the program?

fierce glade
#

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

small steppe
#

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)

fierce glade
#

Character c = 'e'; String numbers = "0123456789ABCDEF";

small steppe
#

Oh

#

e is supposed to be a placeholder

#

It's supposed to be changed

fierce glade
#

c != C

small steppe
#

I know that

#

Again, it's a placeholder cause you can't initialize empty chars

#

Fixed it

#

I had it running backwards lol

fierce glade
#

which value are you passing in?

small steppe
#

1DB