Critical Convertion Dec to Hex

Hi guys!
I'm a begginer in C++ programming and I need a litle help from you...

I'm doing an exercice, and I can't do it completly...
I must convert a decimal number to an hexadecimal number. I dit it, and it works, as follow:

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
{

int value = 355;

char* hex = (char*)malloc(8*sizeof(char));
	int hexIndex = 0;
	for(hexIndex = 0; hexIndex < 8; hexIndex++) hex[hexIndex] = '0';	
	hex[8] = '\0';
	hexIndex = 7;

	
	long hexValue = value;
	long modValue = hexValue % 16;
	hexValue = (long)(hexValue / 16);
	
	PrintString("0x");
	while(hexValue != 0) {
		hex[hexIndex] = getHex(modValue);
		hexIndex--;
		modValue = hexValue % 16;
		hexValue = (long)(hexValue / 16);
	}
	hex[hexIndex] = getHex(modValue);
	PrintResult(hex);

char getHex(long number) {
	if(number >= 0 && number <= 9) return (char)(number+48); 
	return (char)(number+65-10); 
}


No problem here... but I must to do more a thing as a plus:

1 - Be able to convert negative decimal numbers... as -1 -> 0xFFFFFFFF

I'm working in this problem by the last 24hs...
I can't use any native function from c++.

Any help will be great!
Thank you all!
Last edited on
No problem here...
1
2
3
4
5
6
7
	char* hex = (char*)malloc(8*sizeof(char)); //why dynamic allocation? why malloc instead of new? where is the deallocation? 
	hex[8] = '\0'; //out of bounds

char getHex(long number) {
	if(number >= 0 && number <= 9) return (char)(number+48); //magic numbers
	return (char)(number+65-10); 
}
You've got unnecessary castings.

To work with negative numbers, you could try these
_Work with the absolute value, at the end invert all the digits and add 1.
_Transform the negative in the equivalent unsigned.
Hey ne555 thank you very much! You are sure, I took off all unnecessary castings...

Now, how can I invert all digits and transform in the equivalent unsigned?
I'm so sorry but I have no idea how to do this man.... Please can you show me? After so many hours I can't think with some sanity... How can I do this?

Thank you so much!!!!!
Invert the digits (in hexadecimal)
0 -> 15 (F); 1 -> 14 (E); 2 -> 13(D); ...; 15 (F) -> 0
How much do it need to reach 15?

Equivalent unsigned:
Suppose that you only use 1 byte (from 0 to FF)
-1 -> FF; 255 -> FF they have the same representation.
So instead of converting -42 you convert 214
Hello ne555!

I did it:

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
41
42
43
void convertToHexadecimal(long value)
{
        abs(value); // the abs value as you told me

	char* hex = new char();
	int hexIndex = 0;
	for(hexIndex = 0; hexIndex <= 8; hexIndex++) 
	{
		if(hexIndex == 8)
			hex[8] = '\0';
		else
			hex[hexIndex] = '0';
	}
			
	
	hexIndex = 7;

	
	long hexValue = value;
	long modValue = 0;
	
	PrintResult("0x");
	do
	{
		modValue = hexValue % 16;
		hexValue = (hexValue / 16);
		hex[hexIndex] = getHex(modValue);

                hex[hexIndex] |= hex[hexIndex]; //invert the hexadecimal, as you told

		hexIndex--;
		
	}while(hexValue != 0);

	PrintResult(hex);
	PrintNewLine();
}

char getHex(long number) 
{
	if(number >= 0 && number <= 9) return (char)(number+48); 
	return (char)(number+65-10); 
}


Now, how can I add 1 and to know if its negative and print the right value?
This code when I try convert -1, give me the number 0x00000006, and we know that the right answer is 0xFFFFFFFF.

And if I try a number like -741236092, it returns 0x5+5>2*0+ :S

My algorithm is not good, I don't know how to do it....
I must implement al algorithm that converts any decimal, negative or positive, to hexadecimal, print the non-significants zeros, and not use any native function..... I know convert a positive decimal number to hexadecimal, but I don't know how to print the non-significants zeros, and do it with negative numbers..... as you saw I make a for to place '0' in the array....

Please, can you show me the right algorithm? I really need to learn it... I don't see this in a book, and on the web.... please, show me it....

I thank you so much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
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
41
void convertToHexadecimal(long value)
{
        //abs(value); // statement has no effect
        long hexValue = abs(value);

	//char* hex = new char(); //just allocate 1 char
	//char *hex = new char[9]; // However you will still need to deallocated it with delete[]
	char hex[9];

	int hexIndex = 0;
	for(hexIndex = 0; hexIndex <= 8; hexIndex++) 
	{
		if(hexIndex == 8)
			hex[8] = '\0';
		else
			hex[hexIndex] = '0';
	}
	hexIndex = 7;
	long modValue = 0;
	
	PrintResult("0x");
	do
	{
		modValue = hexValue % 16;
		hexValue = (hexValue / 16);
		//hex[hexIndex] = getHex(modValue); //do it at last
                //hex[hexIndex] |= hex[hexIndex]; //That is not invert. And you need to do it before convert 0 -> '0'
             
		hexIndex--;
	}while(hexValue != 0);

	if( value<0 ){
	//Add 1. Sum as you did in elementary school (only that now is in base 16)
	} 

	//convert here
	//if you want to bypass the leading '0', find the first index that hex[i] is not 0 (be careful if input is 0)
	PrintResult( hex+i );
	//PrintResult(hex);
	PrintNewLine();
}
Hello ne555! Thanks for your reply!

Now I can see some light! I still have some doubts:

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
41
42
43
44
45
46
void convertToHexadecimal(long value)
{
       char hexadecimal[9];
	int index = 0;
	long toHex = abs(value);
	long module = 0;

	for(index = 0; index <= 8; index++) 
	{
		if(index == 8)
			hexadecimal[8] = '\0';
		else
			hexadecimal[index] = '0';
	}
	index = 7;
	
	PrintString("0x");
	do
	{
		module = toHex % 16;
		toHex = (toHex / 16);
		
                //when I'll do it?
                //hex[hexIndex] = getHex(modValue); //do it at last
                //hex[hexIndex] |= hex[hexIndex]; //That is not invert. And you need to do it before convert 0 -> '0'

		index--;

                //the loop ends here, when and where I must do:
                //hex[hexIndex] = getHex(modValue);             
		
	}while(toHex != 0);

	if(value < 0)
       {
              //here:  value+=1;
              // you sad: (only that now is in base 16)
              //but if I did not hex[hexIndex] = getHex(modValue);  How I'll work with this?
       }

        //convert here
        //how? I must call getHex here?

	PrintResult(hexadecimal);
	PrintNewLine();
}


Hey ne555, I thank you so much for your patience....
I have 4 hours to finish this exercice...
Another thing, you told that:

 
//hex[hexIndex] |= hex[hexIndex]; //That is not invert. And you need to do it before convert 0 -> '0' 


How can I invert it?

Thanx!
Hey ne555 see it:

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
41
42
43
44
45
46
47
48
49
50
void convertToHexadecimal(long value)
{
       char hexadecimal[9];
	int index = 0;
	long module = 0;
	long toHex = value;

	for(index = 0; index <= 8; index++) 
	{
		if(index == 8)
			hexadecimal[8] = '\0';
		else
			hexadecimal[index] = '0';
	}
	index = 7;

	if(value > 0)
	{
		PrintString("0x");
		do
		{
			module = toHex % 16;
			toHex = (toHex / 16);
			hexadecimal[index] = ToHexCode(module);
			index--;
			
		}while(toHex != 0);

		PrintString(hexadecimal);
		PrintNewLine();
	}
	else
	{
		toHex = abs(value);
		long toHexInverse = ~toHex;
		toHexInverse += 1;
		PrintString("0x");
		do
		{
			module = toHexInverse % 16;
			toHexInverse = (toHexInverse / 16);
			hexadecimal[index] = ToHexCode(module);
			index--;
			
		}while(toHexInverse != 0);

		PrintString(hexadecimal);
		PrintNewLine();
	}
}


I think that I'm to close to resolve this....
Can you help one more time?
Last edited on
I got it!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Hey ne555 I did it man!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

thank you very much!!!!!!!!

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
41
42
43
44
45
46
char hexadecimal[9];
	int index = 0;
	long module = 0;
	long toHex = value;

	for(index = 0; index <= 8; index++) 
	{
		if(index == 8)
			hexadecimal[8] = '\0';
		else
			hexadecimal[index] = '0';
	}
	index = 7;

	if(value > 0)
	{
		PrintString("0x");
		do
		{
			module = toHex % 16;
			toHex = (toHex / 16);
			hexadecimal[index] = ToHexCode(module);
			index--;
			
		}while(toHex != 0);

		PrintString(hexadecimal);
		PrintNewLine();
	}
	else
	{
		toHex = abs(value);
		unsigned toHexInverse = ~toHex;
		toHexInverse += 1;
		PrintString("0x");
		do
		{
			module = toHexInverse % 16;
			toHexInverse = (toHexInverse / 16);
			hexadecimal[index] = ToHexCode(module);
			index--;
			
		}while(toHexInverse != 0);

		PrintString(hexadecimal);
		PrintNewLine();


Man, I don't know how to thank you...... I have not words.....
THANK YOU!
Topic archived. No new replies allowed.