# Hexadecimal to Decimal program giving...unexpected results.

Page 1 of 1

## 6 Replies - 458 Views - Last Post: 10 May 2013 - 05:04 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=320701&amp;s=e9341edc2a05380589eef0cec2fa6adb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bytemyzetta

Reputation: 0
• Posts: 9
• Joined: 20-March 13

# Hexadecimal to Decimal program giving...unexpected results.

Posted 09 May 2013 - 09:23 PM

Hey, I'm in an intro to C++ class and the last chapter we're covering is all about arrays. We have to make some sort of hexadecimal converter and I have no clue what I'm doing wrong.

``` 	for (int i = numberUsed1; i >= 0; i--)
{

int h;
int power = 0;
int result = 0;

h = a[i];
a[i] = h;
h =  (pow(16, power)) * h;
result += h;
power++;

```

This is the code I use to fill the array

```void number (char a[], int size, int& numberUsed)
{
using namespace std;

cout << "Enter up to " << size << " characters. Mark the end with a G \n";
cout << "if you do not intend to use 10 characters in the hexadecimal numeral \n";
cout << endl;
cout << endl;

char next;
int index = 0;
cin >> next;

while ((next != 'G') && (index < size))
{
a[index] = next;
index++;
cin >> next;

}

numberUsed = index;

}

```

When I used a simpler version of the first piece with a self-initialized array, it seems to work fine. I have no idea what I'm doing wrong at all.
```  for (int i = 0; i < 1; i++)
{
int y;
int power = 1;
y = a[i];
a[i] = y;
y = y * pow(16, power);
result = y;

}

```

Is This A Good Question/Topic? 0

## Replies To: Hexadecimal to Decimal program giving...unexpected results.

### #2 jjl

• Engineer

Reputation: 1169
• Posts: 4,785
• Joined: 09-June 09

## Re: Hexadecimal to Decimal program giving...unexpected results.

Posted 09 May 2013 - 09:25 PM

Move your variable declarations out of the for loop. You are resetting your variables to zero after each iteration.

### #3 bytemyzetta

Reputation: 0
• Posts: 9
• Joined: 20-March 13

## Re: Hexadecimal to Decimal program giving...unexpected results.

Posted 10 May 2013 - 02:12 PM

Okay, so I noticed if I initialize my array with B3AD and use this

```  numberUsed1 = 4;
power = numberUsed1 - 1;

do
{
int h;
int result = hexResult1;

h = hex1[i];
hex1[i] = h;
h = h * pow(16, power);
result = h;

hexResult1 += result;

power --;
i ++;

} while (i < 10);

```

I get 45997, which I think is the right decimal equivalent. I guess my problem is when the array isn't filled all the way. Any ideas how to get around that? When I input the characters, it all goes crazy.

### #4 snoopy11

• Engineering ● Software

Reputation: 1129
• Posts: 3,378
• Joined: 20-March 10

## Re: Hexadecimal to Decimal program giving...unexpected results.

Posted 10 May 2013 - 04:14 PM

Hi,

Line 14

while ((next != 'G') && (index < size))
{

should be an OR not an AND comparison there

you are basically saying while next is not a G and index<size in other words both these condtions need to be met to exit the loop.

what you want to say is while next is not a G or index<size then exit this loop.

Regards

Snoopy.

### #5 #define

• Duke of Err

Reputation: 1743
• Posts: 6,200
• Joined: 19-February 09

## Re: Hexadecimal to Decimal program giving...unexpected results.

Posted 10 May 2013 - 04:27 PM

Sorry snoopy11, I think the loop expression is ok. While next is not 'G' and index is less than size then continue with the loop.

In the other loop, you only need to loop over the number of characters present :

```23	} while (i < 10);

```

### #6 snoopy11

• Engineering ● Software

Reputation: 1129
• Posts: 3,378
• Joined: 20-March 10

## Re: Hexadecimal to Decimal program giving...unexpected results.

Posted 10 May 2013 - 04:31 PM

#define, on 10 May 2013 - 11:27 PM, said:

Sorry snoopy11, I think the loop expression is ok. While next is not 'G' and index is less than size then continue with the loop.

In the other loop, you only need to loop over the number of characters present :

```23	} while (i < 10);

```

Damn,

you are right I have a heavy cold....

I have not been posting because of it I should definitely go to bed now...

Thanks

Snoopy.

### #7 #define

• Duke of Err

Reputation: 1743
• Posts: 6,200
• Joined: 19-February 09

## Re: Hexadecimal to Decimal program giving...unexpected results.

Posted 10 May 2013 - 05:04 PM

These expressions can be confusing at the best of times, get well soon.