Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Dec 2 Hex Convert Without Using C++ Commands (http://www.programmingforums.org/showthread.php?t=1351)

RosdaHale Nov 30th, 2004 6:10 AM

I need to convert a Decimal number to HEX (not allowed to use pre-written commands or functions). I have passed the integer decimal in to a function (a) and now need to convert to HEX equivelent. Any1 know of a good method, this is what i tried but cant get it to work. (Need to return a char value!!)

//Function to convert DEC to HEX
int Process_Dec_To_Hex(int a)
{
char Result=0;
char Remainders[7]={0};
int Temp_Remainder=0;

for(int x=6;x>0;x--)
{
Temp_Remainder=a%16;
Remainders[x]=Temp_Remainder;

if(Temp_Remainder==10)
{
Remainders[x]='A';
}
if(Temp_Remainder==11)
{
Remainders[x]='B';
}
if(Temp_Remainder==12)
{
Remainders[x]='C';
}
if(Temp_Remainder==13)
{
Remainders[x]='D';
}
if(Temp_Remainder==14)
{
Remainders[x]='E';
}
if(Temp_Remainder==15)
{
Remainders[x]='F';
}
a=a/2;
}

// To convery the array to a char
for(x=0;x<6;x++)
{
char cBits[7];

for(int i = 0; i<6; i++)
{
cBits[i] = Remainders[i]+0x30;
}
Result=atoi(cBits);
}
return(Result);
}

Eggbert Nov 30th, 2004 10:54 AM

>I need to convert a Decimal number to HEX
If the goal is to take an integral value represented as decimal as an argument and return an integral value represented as hexadecimal then it's as easy as this:
:

int dtoh ( int d )
{
 return d;
}

The only difference between decimal and hexadecimal is how they are printed, so I would imgaine that your task is to return a string with the hexdecimal representation of a decimal value:
:

#include <iostream>
#include <string>

using namespace std;

string dtoh ( unsigned int d )
{
 string ret ( "0x" );

 while ( d != 0 ) {
  ret.insert ( ret.begin() + 2, "0123456789ABCDEF"[d % 16] );
  d /= 16;
 }

 return ret;
}

int main()
{
 cout<< dtoh ( 27 ) <<endl;
}

If std::string is also restricted then you can easily use a C-style string. But keep in mind that the loop builds a number in reverse, so you will need to either prepend items to the string, or reverse the string before returning it.

RosdaHale Nov 30th, 2004 4:34 PM

Dont really understand your explaination sorry, I cant print out the representation I need a string integer that holds the value. I need to pass the decimal in to the function, convert the decimal to hex, and then return the hex result as a string.

Basically i have HexNum=Process_Numbers(Dec_Num) (where Dec_Num is a integer)
and then the Process_Numbers(string a) to convert it and return the Hex string. I presume I would have to convert the Dec_Num integer to a string first before i pass it ?? i dont know.

Eggbert Dec 4th, 2004 9:44 AM

>I need to pass the decimal in to the function, convert the decimal to hex, and then return the hex result as a string.
That's precisely what the example I gave you does. It accepts an integer value as an argument, then builds a string with the hexadecimal representation of the integer and returns it. I can understand how you would have trouble. If you specify which part of the algorithm is troublesome I'll explain its workings in detail.


All times are GMT -5. The time now is 2:46 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC