### Decimal to Hexadecimal converter

Hey guys, I would like to get your feedback on my dec to hex converter. I've been absent from this site for a while but that's irrelevant.

I would also like to know if anyone knows of an easier way to do this conversion manually not using a conversion function in a header file, but making the conversion mathematically.
Thank you in advance.

p.s sorry for the `clrscr();` I use it to make everything neater, but if you don't have the conio library just replace it with a `cout<<endl<<endl;` I guess. :(

Code:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include #include using namespace std; void Dec_To_Hex(unsigned int); int main() { unsigned int dec; string loop; do{ cout<<"Please enter the number you would like to convert to hexadecimal."<>dec; Dec_To_Hex(dec); cout<>loop; cout<=1) { c=10;c2=0; if ((dec%16)<10) { while (c2!=dec%16) { c2++; } hex[c3]=number[c2]; } c2=0; if ((dec%16)>=10) { while (dec%16!=c) { c2++; // to get actual letter c++; // to help get place of letter[c] in letter[6] array } hex[c3]=letter[c2]; } dec/=16; c3++; // going on to next position in the array for the output of hex conversion } while (c3>=0) { // Array needs to be displayed backwards if (c3<=0) goto end; c3--; cout<
Last edited on
I'm sorry to break your heart...
Try this out.

 ``12345678910111213`` ``````#include #include int main () { int i; char buffer [33]; printf ("Enter a number: "); scanf ("%d",&i); itoa (i,buffer,16); printf ("hexadecimal: %s\n",buffer); return 0; }``````
Last edited on
Why printf?
Is C format better?
WOW! well that would have made my conversions much easier, I hadn't known about the itoa function.
so i is the number you want to convert and buffer is the number converted?
that sound about right?
guatemala007 wrote:
WOW! well that would have made my conversions much easier, I hadn't known about the itoa function.
so i is the number you want to convert and buffer is the number converted?
that sound about right?

Yeah, `i` is the value to be converted to string, `buffer` is the array in memory where to store the resulting null-terminated string, and `16` is the numerical base used to represent the value as a string, between 2 and 36, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary.
well apart from the itoa function is there a more efficient way to do the conversion manually?
 ``12345678910`` ``````#include #include int main() { int input ; std::cout << "Enter decimal number: " ; std::cin >> input ; std::cout << "0x" << std::hex << input << '\n' ; }``````

Because the C++ code is hardly legible!
Ok well thanks usandfriends and cire that helps alot in the future. But I am still wondering if there is a better way to do it manually?

oh and cire what makes it hardly legible?
Well, you could replace this:
 ``123456`` `````` int i; char buffer [33]; printf ("Enter a number: "); scanf ("%d",&i); itoa (i,buffer,16); printf ("hexadecimal: %s\n",buffer);``````

with this:
 ``1234`` `````` int i; cout << "Enter a number: "; cin >> i; cout << hex << i;``````

(note, `itoa()` is non-standard and may not be available)

Edit: my apologies, I see the original post states: "manually not using a conversion function in a header file".
Last edited on
yeah Chervil I got that from cire's last response, but thanks anyways.

Edit: that's ok, I probably should have made that bold, I guess I'll go do it now.
Last edited on
Chervil wrote:
Edit: my apologies, I see the original post states: "manually not using a conversion function in a header file".

Did not see that either guatemala.

1. conio -> conio.h
2. stdio -> cstdio/stdio.h

Your library names do not exist. Change them to include the proper extension.
Last edited on
One thing I would suggest. There are two separate arrays:
 ``12`` ``````const char letter[6]={65,66,67,68,69,70}; const char number[10]={48,49,50,51,52,53,54,55,56,57};``````

I think the task would be much simpler if just a single array was used:
 `` `` `` const char hexchar[17] = "0123456789ABCDEF";``

Position 17 is for the null terminator, that location won't actually be needed during the conversion.
OK thanks Chervil, I hadn't thought about that, it would make things much simpler especially for my if statements in my while loop. I'll try it out thanks.
Last edited on
usandfriends You can just take out the stdio library. Although I don't know why it's not working for you. I'll re-edit my post without it and fix the conio.h thing.
Decimal to Hexa Conversion

I tried using do-while loop and nested else-if statement, but there's something wrong with my code. Help me.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include using namespace std; void hexaD(int a, char b); int main() { int x; hexaD(x, ' ') return 0; } void hexaD(int a, char b) { int hexa[2]={0}, x,y,i=1; cout << "Enter the value to convert to hexa: "; cin >> x; y=x; do { hexa[i] = y%16; if (y==10) cout << 'A'; else if (y==11) cout << 'B'; else if (y==12) cout << 'C'; else if (y==13) cout << 'D'; else if (y==14) cout << 'E'; else if (y==15) cout << 'F'; else; y=y/16; i--; }while(y>0); for(i=0;i<2;i++) cout << hexa[i]; } ``````

I get the wrong output if I put 255 as my input, it would produce F1515. What should I do?

@klutzslouch There are two parts to your code. The first where the value is stored in the array, and the second where you loop through the array and print it out. Some of the code which belongs in the second section was in the first instead. I've moved it to the right place.

There are still a few problems with this code, first the parameters a and b are never used. More importantly, what happens if the number has more than two hexadecimal digits.

But still, the code below might point you in the right direction a little bit. One thing you might consider is to use an array of characters instead of an array of integers. Then you could store the required character A to F or 0 to 9 in the array. Add a null terminator '\0' after the final character and there you have the result neatly stored as a string. Make sure the array is big enough!

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````void hexaD(int a, char b) { int hexa[2]={0}, x,y,i=1; cout << "Enter the value to convert to hexa: "; cin >> x; y=x; do { hexa[i] = y%16; y = y/16; i--; } while(y>0); for (i=0;i<2;i++) { y = hexa[i]; if (y==10) cout << 'A'; else if (y==11) cout << 'B'; else if (y==12) cout << 'C'; else if (y==13) cout << 'D'; else if (y==14) cout << 'E'; else if (y==15) cout << 'F'; else cout << y; } }``````
Thanks Chervil!
Topic archived. No new replies allowed.