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!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!