![]() |
|
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Newbie
Join Date: Nov 2004
Posts: 15
Rep Power: 0
![]() |
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); } |
|
|
|
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|