convert to hexadecimal and hex addition

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

 using namespace std;

string m[6];string ye;ostringstream wa;
string momo;
//pass function result to main
void con(int n)
{
    int a,i,j; int o=0;
    int b[6];

           for(i=0;n!=0;i++)//get the number of digits(i)and number on digit(b[i])
    {
        a=n%16; //remainder,which is the right most digit;
        b[i]=a; //b[0]=right most digit,b[1]=second digit...
        n=n/16; //the quotient used for the next iteration unless it is equal to 0, then there will be no more iteration.
    }
    
    for(j=i-1;j>=0;j--)//the loop to print out array
    {

        
            
        
        if(b[j]==10)
        {
           
            m[o]="A";
            
        }
        else if(b[j]==11)
        {
         
            m[o]="B";
        }
        else if(b[j]==12)
        {
         
            m[o]="C";
        }
        else if(b[j]==13)
        {
          
            m[o]="D";
        }
        else if(b[j]==14)
        {
                  }
        else if(b[j]==15)
        {
       
        }
        else
        {
           
         m[o]=b[j] +'0';
    
 
        }
        
        wa<< m[o];
        o++;
  
        

        }

    }
    




void addhex(hex[],hex[]){

}
 



    int main(){
        int x,y;
        string ha[];int hb[];
        
        cout<<"Enter 2 integers:"<<endl;
        
        cin>>x>>y;
        con(x);
        
        cout<<"The first number is"<<setw(6) << setfill('0')<< wa.str()<<endl;
        wa.str
        
        wa.str().clear();
        
        con(y);
    
 
        
        cout<<"The second number is"<<setw(6) << setfill('0')<< wa.str()<<endl;

     

        addhex(ha[],hb[]);
      
        
        
        return 0;
        
    }


1)What should be the best variable for adding two 6-digit hexadecimal,such as 0034AD,0057EA? I would like to use array of character but it seems hard to handle.



Thanks a lot~~~
Last edited on
int a,b;

cin >> hex >> a;
cin >> hex >> b;

cout << hex << a+b << endl;
but the rule for add two hexadecimal numbers is :

"similar to adding two decimal numbers, except that a carry will be generated if the sum is > 16, instead of 10. You start from the rightmost digit, add the digits together. If the result is greater than 16, and there is a carry and you subtract from the result by 16. "
hexadecimal is not a number, but a number representation.

if you have number like:
123 decimal (base of 10)
it is the same as
0x7B (base of 16)
173 (base of 8)
or
1111011 (base of 2)

123 + 123 is the same as 0x7B + 0x7B or 1111011B + 0x7B.
Topic archived. No new replies allowed.