![]() |
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); } |
>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 ):
#include <iostream> |
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. |
>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