Reputation: 31
I have a program I am writing that converts Octal to Decimal numbers. Most of it works. (more code above this, assume all variables are properly declared).
for(i; i > 0; i--)
{
decimalNumber = (decimalNumber + (number['i'] * pow(8,power)));
power++;
}
The code correctly shifts over to the right to do other digits but it doesn't change the number it is working with. For example, entering 54 in octal results in an output of 36, 4*(8^0) + 4*(8^1) when it should be outputting 4*(8^0) + 5*(8^1), or 44.
Upvotes: 0
Views: 249
Reputation: 40145
number[0] is 5
number[1] is 4
decimalNumber is 0
power is 0
i = 1 downto 0 do
decimalNumber = (decimalNumber + (number[i:1,0] * pow(8,power:0,1)));
power++;
do end
Upvotes: 0
Reputation: 5093
As Ignacio pointed out, 'i'
is a constant and will cause you to access the same out of bounds array element on each iteration of the loop. Since I assume you start with i
equal to the number of digits in the array (you didn't show that code), you want to subtract 1 from it when you use it as an array index.
Upvotes: 1
Reputation: 183948
You're traversing the string in the wrong direction. Or, better, change your logic:
5 -> 5*8^0
54 -> (5*8^0)*8 + 4
543 -> ((5*8^0)*8 + 4)*8 + 3
Upvotes: 0
Reputation: 798884
'i'
is a constant. You probably meant just i
. Also, << 3
.
Upvotes: 3