• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Java question: WTF am I doing wrong here? ***Updated: I'm a retard and it's fixed now. FREEWARE for all!***

silverpig

Lifer
Maybe I'm blind or something, which is entirely possible as I've been staring at this code for a while.

I'm supposed to write an app that converts numbers from base X to 10 and 10 to X. I have the 10 to X part fine, but here's my code for the X to 10. It gives me very very wrong results.


public String ConvertX(String inNumber1, int base1) //"inNumber" is the input string. ie CA2 in base 16 or 1101001 in binary. "base1" is the number base that "inNumber" is in.
{
out2.setText("");
int length = inNumber1.length();
int value = 0;
int u = 0;
int p = length - 1;

for (int k = 0; k < length; k++)
{
char ch = inNumber1.charAt(k); //get the digit

if((ch>= '0') && (ch <= '9')) //convert it to an integer
u = ch - '0';
else if (ch == 'A')
u = 10;
else if (ch == 'B')
u = 11;
else if (ch == 'C')
u = 12;
else if (ch == 'D')
u = 13;
else if (ch == 'E')
u = 14;
else if (ch == 'F')
u = 15;

if (u > base1) //error check... this works fine
return new String("Invalid");

value = value + (Math.pow(base1, p) * u); // I'm pretty sure this is what's getting me messed up. If change this line to value = u, it displays the value in base 10 of the last digit, which is fine.

p--;
}
 
Here's the code for a perl script I wrote that does the same thing, only it converts from any base (X) to any other base (Y), base 10 is not required.

#!/usr/bin/perl

# convert number bases.

# usage: base.pl [input base] [output base] [input value]
# Example: convert from 567 base 10, to binary
# base.pl 10 2 567

$sbase = $ARGV[0];
$ebase = $ARGV[1];
$value = $ARGV[2];

# standardize starting number (base 10)

# prepare values to be played with.
chomp $value;
$value = lc $value;
@values = split //, $value;
@values = reverse @values;

for(my $i = 0; $i < @values; $i++){
if($values[$i] =~ m/[a-z]/){
$values[$i] = ord($values[$i]) - 87;
}
}

# standardize starting number (base 10)
$mult = 1;
$i = 0;
while($i < @values){
$values[$i] *= $mult;
$mult *= $sbase;
$i++
}
my $base10 = 0;

for(my $i = 0; $i < @values; $i++){
$base10 += $values[$i];
}

# convert base 10 to final base
$output = '';

while($base10 >= $ebase){
$temp = $base10 % $ebase;
$base10 -= $temp;
$base10 /= $ebase;
if($temp > 9){
$output = chr($temp + 55) . $output;
}
else{
$output = $temp . $output;
}
}

if($base10 > 9){
$output = chr($base10 + 55) . $output;
}
else{
$output = $base10 . $output;
}

# print output
print "$output\n";
 
I guess it really does do the same thing, since it converts everything to base 10, and then converts that to the final base....
 
Originally posted by: notfred
I guess it really does do the same thing, since it converts everything to base 10, and then converts that to the final base....

Pretty much yeah. I know how to do it, I just can't get the stupid value = line to work right.
 
Originally posted by: silverpig
Originally posted by: notfred
I guess it really does do the same thing, since it converts everything to base 10, and then converts that to the final base....

Pretty much yeah. I know how to do it, I just can't get the stupid value = line to work right.

this section of the perl script:

# standardize starting number (base 10)
$mult = 1;
$i = 0;
while($i < @values){
$values[$i] *= $mult;
$mult *= $sbase;
$i++
}

Does the same thing as what your line is tring to do (adjust the decimal value according to the last digit). Then it adjusts the amount of the next adjsutment (the amount of the adjustment will increase by a factor of the base you're working on for each digit from the right.)
 
Okay. Little more info.

I can convert 1 to F in base 16 just fine.
Once I input the number "10" in base 16 (or 16 in base 10), I get 256. Obviously this is 1* 16^2. I want 1 * 16^1. Why the heck is p = 2 here?

Same happens later on. 100 is supposed to equal 1 * 16^2, but I get 1 * 16^4.
 
there is probably some character after the input. maybe a return/newline, try to filter the input first


hard coding in the input works properly. so tha tmust be it
 
Yay! I fixed it.

I had to change

int value = 0;

to

double value = 0;

and now it works just fine. I guess it got messed up in the int - double conversion in the pow function.

Thanks all.
 
I seperate all the numbers, by digit, into an array of integers.

For example, covert hex to dec: F2A

You get an array (note that the array is in ascending order of significance) that looks like this:

array[0]: 10
arary[1]: 2
array[2]: 15


You declare an int with the value of the current digit:
int valDigit = 1;

and an int for the value computed
int valNumber = 0;

So, you go through the array:

for(int i = 0; i < array.length; i++){

for each number, multiply it by the value of the current digit, and add it to the final value.
valNumber += (i * valDigit);

Then set the value for the next digit
valDigit *= base // where base, in this case, is 16.

}

I think I typed that all right, but it could be wrong as my fiancee's been talknig about weddings or something to me the whole time.
 
Originally posted by: silverpig
Yay! I fixed it.

I had to change

int value = 0;

to

double value = 0;

and now it works just fine. I guess it got messed up in the int - double conversion in the pow function.

Thanks all.

wtf that's not supposed to happen. it doesn't fvcking make sense LOL!

i did use the cast of (int)Math.pow() cuz without the cast JDK won't compile but still, wtf?!
 
Originally posted by: dighn
Originally posted by: silverpig
Yay! I fixed it.

I had to change

int value = 0;

to

double value = 0;

and now it works just fine. I guess it got messed up in the int - double conversion in the pow function.

Thanks all.

wtf that's not supposed to happen

though i did the cast of (int)Math.pow() cuz without the cast JDK won't compile

It compiled for me without the cast. Odd. I suppose it'd work for me if I did the cast too. Maybe that's what I'll do actually as it gives me a number with a decimal point as output.
 
Originally posted by: silverpig
Originally posted by: dighn
Originally posted by: silverpig
Yay! I fixed it.

I had to change

int value = 0;

to

double value = 0;

and now it works just fine. I guess it got messed up in the int - double conversion in the pow function.

Thanks all.

wtf that's not supposed to happen

though i did the cast of (int)Math.pow() cuz without the cast JDK won't compile

It compiled for me without the cast. Odd. I suppose it'd work for me if I did the cast too. Maybe that's what I'll do actually as it gives me a number with a decimal point as output.

still, it makes now sense how iwhtout the cast it gives you an aswer that's shifed by 1 place!? i dont see how that would happen... i mean value i snot involved in the exponent. have u tried other bases? does it always shift by 16 ie 4 bits? maybe it doesn't do implicit conversion and somehow interets the double as an int directly
 
That's why I was all messed up over this. I didn't know why it kept shifting. It did that with all the bases. Even base 10. I'd put 10 in and get 100 out, 100 in and get 10000 out.
 
i did that for a logic class last semester... well, any radix to any radix. too bad i couldnt use integer.radix =P
 
Back
Top