error in coverting from decimal to hexadecimal

I'm trying to write a program that covert a hexadecimal number to a decimal number, but i don't know what is the error (a logical error) in this program, any help please!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream.h>
#include<string.h>
using namespace std;
string dec_to_hex(long int num);

int main()
{
	long int num ; // the number in dec
	cin >> num ;
	cout << dec_to_hex(num) ; // the number in hex
	return 0 ;
}

string dec_to_hex(long int num)
{
	string hex ; // the program will return (hex)
	char z ; // z will be added to hex

	long int x=num ;
	int c ;
	for(c=0;x!=0;c++) // c is the number of digits
	x/=10 ;
	
	char array[c] ;
	
	for(int i=c-1;num!=0;i--)
	{
		int mod = num%16 ;
		
		if(mod>=0 && mod<=9)
		z=char(mod+48) ;
		else
		z=char(mod+55) ;
		
		array[i]=z ; // to add the char in it's place in the array
		hex.append(array) ; //to add the array digit in the string 
		num/=16 ;
	}
	return hex ;
}
Last edited on
array doesn't make overly sense.
remove it and hex could be build like so:
hex = z + hex; //to add the array digit in the string
Are you writing in C99 or C++?
Your include files and using namespace std; implies this is C++.

Line 24: Allocating an array requires a const which can be evaluated at compile time in C++. Allocating an array with a run time value is only valid in C99. As coder777 said, array isn't even needed.

Also, your include files should be simply <iostream> and <string>. The .h suffix is deprecated in C++, although some very old C++ compilers still use the .h suffix.

thnx coder777 and AbstractionAnon .. but the result is fliped! should I flip the decimal number befor converting, or there is an error in the loop.

AbstractionAnon I'm using a bad compiler "C-Free", but I should use it untill I finish the course in the university.

I don't know what is C99! But I learn that way from the internet.
In your loop at line 28, you're taking hex digits off from the right so you need to insert the hex characters in the from of hex, which is what coder777 suggested. I suggest you post your current code.

C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999
http://en.wikipedia.org/wiki/C99
this is the current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string dec_to_hex(long int num)
{
	string hex; 
	char z;
	
	while(num!=0)
	{
		int mod=num%16;
		
		if(mod>=0 && mod<=9)
		z=char(mod+48);
		else
		z=char(mod+55);
		
		hex=hex+z;
		num/=16;
	}
	return hex;
}
Line 15: You're appending the hex digit to the end (right) of the hex string. That's going to result in the digits being flipped.

You're taking off increasingly significant digits each time thru the loop, but putting them at the right end.

Change line 15 the way coder777 suggested.
1
2
 
		hex=z+hex;


oh! I I thought that it is the same!
thanks for helping, problem solve.
Topic archived. No new replies allowed.