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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>
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."<<endl;
cout<<"(MIN = 1, MAX = 4,294,967,295)"<<endl;
cin>>dec;
Dec_To_Hex(dec);
cout<<endl;
cout<<"Would you like to convert another number\? (Yes/No)"<<endl;
cin>>loop;
cout<<endl;
}while (loop=="Yes"||loop=="yes"||loop=="Y"||loop=="y");
cin.get();
return 0;
}

void Dec_To_Hex(unsigned int dec)
{
int c=10,c2=0,c3=0;  //counter variables
const char letter[6]={65,66,67,68,69,70};
const char number[10]={48,49,50,51,52,53,54,55,56,57};
char hex[8];
cout<<"Your number in hexadecimal form is: "<<endl;
while (dec>=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<<hex[c3];
}
end:
}
Last edited on
closed account (18hRX9L8)
I'm sorry to break your heart...
Try this out.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <stdlib.h>

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?
closed account (18hRX9L8)
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?
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <iomanip>

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:
1
2
3
4
5
6
    int i;
    char buffer [33];
    printf ("Enter a number: ");
    scanf ("%d",&i);
    itoa (i,buffer,16);
    printf ("hexadecimal: %s\n",buffer);


with this:
1
2
3
4
    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
closed account (18hRX9L8)
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.

Tested your program:
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:
1
2
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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.