Programming Forums
User Name Password Register
 

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

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Nov 30th, 2004, 6: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
 

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 5:47 PM.

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