read it backwards

Hi
my program is converter to hexadecimal number and it works fine but there is one mistake. I need code to make the output of convert function to print backwards. so it would be indeed hexadecimal number. please help.
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
#include <iostream>

using namespace std;
void convert(int num);

int main()
{
    
    int num;
    
    cout <<"insert the number you want to convert"<<endl;
    cin>> num;
    cout << "you inserted number "<<num<<endl;
    cout << "hexadecimal is: ";
    convert(num);
 
    system ("pause");
    return 0;
}

void convert (int num)
{
     int rest;
     while (num!=0)
      {
          rest = num % 16;
          num = num/16;
          if (rest==10)
          {
              char a ='A';
              cout << a;
          }
          else if (rest==11)
          {
              char a ='B';
              cout << a;
          }
          else if (rest==12)
          {
              char a ='C';
              cout << a;
          }
          else if (rest==13)
          {
              char a ='D';
              cout << a;
          }
         else  if (rest==14)
         {
              char a ='E';
              cout << a;
         }
         else if (rest==15)
         {
              char a ='F';
              cout << a;
         }
         else if (rest<10)
              cout <<rest;  
      }
     cout << endl;
}


and yes i know that i could use
cout <<hex<<num;
but i wanted to do it on my own. and i am stuck now. and also so many if statements don t look so good. any idea how to make it shorter??
Here's one way to do 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
#include <iostream>
#include <string>

using namespace std;
void convert(int num);

int main()
{
    
    int num;
    
    cout <<"insert the number you want to convert"<<endl;
    cin>> num;
    cout << "you inserted number "<<num<<endl;
    cout << "hexadecimal is: ";
    convert(num);
    cout << '\n' ;
 
    system ("pause");
    return 0;
}

void convert (int num)
{
    static char const hexChar[] = "0123456780ABCDEF" ;

    if ( num != 0 )
    {
        convert( num/16 ) ;
        std::cout << hexChar[num%16] ;
    }
}

<almost duplicate post, deleted>

@mirec: TODO: modify cire's code to
a. take care of num being negative
b. take care of num being originally zero.
Last edited on
ok i am confused now. when it get to convert function and to line 29 it should start convert function all over again and it never gets to cout<< hexChar[num%16] ... so why it works?? and why it print out the last num%16 first?? in cire s code ..not in this below.

and my second question is if the negative hexadecimal numbers are the same as positives just with negetive sign infront of it

and this is what i have done:
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
#include <iostream>
#include <string>

using namespace std;
void convert(int num);
void convert2(int num);
int main()
{
    
    int num;
    cout <<"insert the number you want to convert"<<endl;
    cin>> num;
    cout << "you inserted number "<<num<<endl;
    cout << "hexadecimal is: ";
    if (num>0 )
    convert(num);
    if (num == 0)
    cout << "0";
    if (num < 0)
    convert2(num);
    cout << "\n" ;
 
    system ("pause");
    return 0;
}

void convert (int num)
{
    static char const hexChar[] = "0123456789ABCDEF" ;

    if ( num !=0 )
    {
        convert( num/16 ) ;  
        cout << hexChar[num%16] ;
    }                   
}


void convert2(int num)
{
       // I don t know what value is negetive hexadecimal.  
}


and yeah thank you guys.. both of you...
Last edited on
hello,, guys.. please..
> it should start convert function all over again
> and it never gets to cout<< hexChar[num%16] ... so why it works??

It would get to cout<< hexChar[num%16] when the called function returns.
See: http://www.cprogramming.com/tutorial/lesson16.html


> if the negative hexadecimal numbers are the same as positives just with negetive sign infront of it

Typically, when a hexadecimal number is printed, each hex digit is a representation of the value of four bits. We make no distinction betwen a sign bit and other value bits. For instance, a 32-bit signed integer with one sign bit and 31 digit bits is treated as eight four-bit values. In other words, the signed int is treated as an unsigned int.

In printing out the hexadecimal literal, hexChar[num%16] is a technical error when num is a signed integer. It leads to undefined behaviour when num is negative. What would -17%16 yield?
1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    // what does this print out?
    std::cout << std::hex << -17 << '\n' ;

    // and what would this print out?
    std::cout << (-17/10) << (-17%16) << '\n' ;
}


The simplest solution is to just make the function interpret the number as an unsigned value:
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
#include <iostream>

void print_hex( unsigned int num ) ;

int main()
{
    int num ;
    while( std::cout << "number? " && std::cin >> num )
    {
        std::cout << "decimal: " << num <<  "  hex: 0x" ;

        if( num == 0 ) std::cout << '0' ;
        print_hex(num) ;
        std::cout << '\n' ;
    }
}

void print_hex( unsigned int num )
{
    static constexpr char digits[] = "0123456789abcdef" ;

    if ( num != 0 )
    {
        print_hex( num/16 ) ;
        std::cout << digits[ num%16 ] ;
    }
}
Last edited on
thanks JLBorges
so ok. i tryed cout<< hex <<-17; and it gave me some fff crap and the other one some letters i never even set before.

but I finished it of like this.. what do you think?

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
#include <iostream>
#include <string>

using namespace std;
void convert(int num);
void convert2(int num2);
int main()
{
    
    int num;
    cout <<"insert the number you want to convert"<<endl;
    cin>> num;
    cout << "you inserted number "<<num<<endl;
    cout << "hexadecimal is: ";
    if (num>0 )
    convert(num);
    if (num == 0)
    cout << "0";
    if (num < 0)
    {
    num-=2*num;
    unsigned int num2 = num;
    cout <<"-";
    convert2(num2);
}
    cout << "\n" ;
 
    system ("pause");
    return 0;
}

void convert (int num)
{

    char hexChar[] = "0123456789ABCDEF" ;

    if ( num !=0 )
    {
        convert( num/16 ) ; 
        cout << hexChar[num%16] ;
    }                   
}  


void convert2(int num2)
{
    
      char hexChar[] = "0123456789ABCDEF" ;

    if ( num2 !=0 )
    {
        convert( num2/16 ) ; 
        cout << hexChar[num2%16] ;
    }       
}


whenever i didn t use my line 21 and 22 it didn t work it changes it to some huge number every time. but like this it works.. :)

and if i may ask can you guys give me some assignement to do for this work or something totaly else so i can keep improving myself?? thx
> i tryed cout<< hex <<-17; and it gave me
some fff crap and the other one some letters i never even set before.

std::cout << std::hex << -17 << '\n' ; will print out something like ffffffef.
The same as what std::cout << std::hex << unsigned(-17) << '\n' ;
or std::cout << std::hex << -17U << '\n' ; would print out.

In general, avoid using the modulo operator on numbers that could possibly be negative. The rules are different in different programming languages, and what you learn in one would have to be unlearned in another.
See http://en.wikipedia.org/wiki/Modulo_operation#Remainder_calculation_for_the_modulo_operation

If you want to print out negative values with a minus sign prefixing the positive value:

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
#include <iostream>

void print_hex( unsigned int num ) ;

int main()
{
    int num ;
    while( std::cout << "number? " && std::cin >> num )
    {
        std::cout << "decimal: " << num <<  "  hex: " ;

        if( num == 0 ) std::cout << '0' ;
        else if( num < 0 ) // negative
        {
            std::cout << '-' ; // prefix a minus sign
            num = -num ; // drop the minus sign
        }
        print_hex(num) ; // invariant: num is non-negative
        std::cout << '\n' ;
    }
}

void print_hex( unsigned int num )
{
    static constexpr char digits[] = "0123456789abcdef" ;

    if ( num != 0 )
    {
        print_hex( num/16 ) ;
        std::cout << digits[ num%16 ] ;
    }
}
ok so basicaly is the same thing as i did. I just didn t know what u can drop the minus sign like that.. and i did it different way... but it works.. :)

ok thanks JLBorgers
Topic archived. No new replies allowed.