Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2004, 5:10 AM   #1
RosdaHale
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 RosdaHale is on a distinguished road
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);
}
RosdaHale is offline   Reply With Quote
Old Nov 30th, 2004, 9:54 AM   #2
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>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.
Eggbert is offline   Reply With Quote
Old Nov 30th, 2004, 3:34 PM   #3
RosdaHale
Newbie
 
Join Date: Nov 2004
Posts: 15
Rep Power: 0 RosdaHale is on a distinguished road
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.
RosdaHale is offline   Reply With Quote
Old Dec 4th, 2004, 8:44 AM   #4
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 4 Eggbert is on a distinguished road
>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.
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:13 PM.

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