Base 10 to base 16

Dec 10, 2019 at 4:43am
Hello so I have this problem/homework for uni and it's due today and I am completely stuck.
It says: Write a program which transforms from base 10 to base 16 integer+ numbers of max 9 numbers. The number in base 10 will be read from the keyboard and the result will be written in a file with the name HEXA.OUT which will contain on the first row the number in base 10 and on the second one the number in base 16.
Can someone help me with this asap I would really appreciat it^^
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
67
68
69
  #include <stdio.h>
    #include <fstream>
    #include <iostream>
    using namespace std;

    int convert(int numar, int baza)
    {

        if (numar == 0 || baza == 10)
        {
            return numar ;
        }
        return (numar % baza) + 10 * convert(numar / baza, baza);


}
int switchToLetter(int numar)
{
    switch (numar)
    {
    case 10:
        cout << 'A';
            break;
    case 11:
        cout << 'B';
            break;
    case 12:
        cout << 'C';
            break;
    case 13:
        cout << 'D';
            break;
    case 14:
        cout << 'E';
            break;
    case 15:
        cout << 'F';
            break;
    }
}
int main()
{
    int numar;
    int baza;
    ifstream numere("HEXA.OUT.txt");
    HEXA.OUT.open("HEXAOUT.txt");
    if (HEXA.OUT_open())
         cout << "open";
    else
         cout << "not open";
    int inputvalue = numar;
    if (inputvalue > 1000000000)
    {
        cout << "Decimal numar too large for this program" << endl;
    }
    else
    {

        do
        {
            convert(numbar, baza);
            cout << inputvalue << "in baza" << baza << "is" << numar << endl;

        } while (inputvalue >= 0);
    }

    return 0;
}
Dec 10, 2019 at 9:09am
C++ does this for you, on demand.
1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
  int value;
  std::cout << "Enter value: ";
  std::cin >> value;
  std::cout << std::hex << "Value in hex: " << value;
}

Dec 10, 2019 at 5:15pm
I guess, your teacher wants that you make the converting by hand and without the help of converting tools.
Also, it seems that you have still problems of correct using the streams library.

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
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>

std::string convert( unsigned long long num, unsigned base )
{
    if (base < 2 || base > 16) return "Unsupported base!";
    if (num == 0) return "0";
    
    std::string result;
    while (num)
    {
        int digit = num % base;
        num = num / base;
        
        switch (digit)
        {
            case 0: case 1: case 2: case 3: case 4:
            case 5: case 6: case 7: case 8: case 9:
                result = std::to_string(digit) + result;
                break;
            case 10:
                result = "A" + result;
                break;
            case 11:
                result = "B" + result;
                break;
            case 12:
                result = "C" + result;
                break;
            case 13:
                result = "D" + result;
                break;
            case 14:
                result = "E" + result;
                break;
            case 15:
                result = "F" + result;
                break;

        }
    }
    return result;
}

unsigned long long read_number()
{
    unsigned long long num;
    std::string tmp;
    while (true)
    {
        std::getline( std::cin, tmp);
        try { num = std::stoull( tmp ); }
        catch (std::exception & e) { continue; }
        break;
    }
    return num;
}

        
int main()
{
    std::string filename = "HEXA.OUT";
    std::ofstream ofstr( filename );
    
    unsigned long long number;
    while (true)
    {
        std::cout << "Enter a number between 0 and 10000000000\n";
        number = read_number();
        if ( number < 10000000000 ) break;
    }
    ofstr << number << '\n';
    ofstr << convert(number, 16);
}
Last edited on Dec 10, 2019 at 5:16pm
Dec 10, 2019 at 8:49pm
you are working way too hard. Stop and think about the math. Peel off one hex digit at a time, same as you would in base 10 (1234 .. mod 10 is 4, /10 is 123, repeat, 3 and 12, ... then you have it in reverse 4321 and just print in reverse... same thing works in hex). 4 lines (while to reverse). 5 if you count the constant lookup table.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
     unsigned int i = 123456;
     string s;
     char c[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
     while(i)
     {
	   s+= (char)c[i%16];
	   i/=16;
	 }		 
	 reverse(s.begin(),s.end());
	 cout << s << endl;
	return 0;
}


you can make it even smaller if you use recursion to reverse it.
Last edited on Dec 10, 2019 at 8:54pm
Dec 11, 2019 at 9:42am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

string toBase( unsigned n, unsigned base )
{
   const string chars = "0123456789ABCDEF";
   return n < base ? chars.substr( n, 1 ) : toBase( n / base, base ) + chars[n%base];
}

int main()
{
   unsigned n;
   cout << "Input n: ";   cin >> n;
   cout << toBase( n, 16 );
}
Topic archived. No new replies allowed.