From decimal to hexa. haloa guys, i've a problem on a code in c++.

Feb 26, 2014 at 8:40pm
Hi, i'm new on this forum.
I have to make a program that transform a decimal number to hexadecimal. The code works only for some decimal values. Like 4095, 3058, 405, 1315 ..... but it does not work for many other values ​​like: 3586, 3584 ...
I do not know which saint to pray so I thought I'd ask you to help.
thanks for any suggestions and help.

#include <iostream>

using namespace std;

int main()
{ int a,c;

cin>>a;
c=144;
while (c>16)
{
if (a>16 && a<256)
{

c=a%16;
a=a/16;
switch (a)
{
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;
default:
cout<<a;
break;

}
switch (c)
{
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;
default:
cout<<c;
break;
}
}
if (a>256 && a<4096)
{
c=a%256;

a=a/256;

switch (a)
{
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;
default:
cout<<a;
break;

}



}

if (a>4096)
{
c=a%4096;

a=a/4096;

switch (a)
{
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;
default:
cout<<a;
break;

}
}
a=c;
}


cout<<endl;


system("PAUSE");
return 0;
}


P.S.
(where are the smile in this forum?)


Feb 26, 2014 at 9:43pm
It is easier to rewrite that code from scratch than to fing logic error in it.
For example:
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
#include <array>
#include <iostream>
#include <string>


std::string to_hex(unsigned number)
{
    static const std::array<char, 16> hexdigits =
        {'0', '1', '2', '3', '4', '5', '6', '7',
         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',};
    std::string hex;
    while (number != 0) {
        hex.push_back(hexdigits[number % 16]);
        number /= 16;
    }
    return {hex.rbegin(), hex.rend()};
}


int main()
{
    unsigned number;
    std::cin >> number;
    std::cout << to_hex(number) << std::endl;
}
http://ideone.com/uEb3O4
Last edited on Feb 26, 2014 at 9:43pm
Feb 27, 2014 at 5:55pm
Minipaa, I've to do an exercise that transform decimal to hexadecimal whiteout shortcuts, the challenge is this. Can you help me, and analyze the code for find the error. Thank
Feb 27, 2014 at 7:06pm
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
#include <iostream>

void print_hex_digits( unsigned int n )
{
    static const unsigned int RADIX = 16 ;
    static const char hex_digits[] = "0123456789abcdef" ;

    char digit = hex_digits[ n % RADIX ] ;
    if( n >= RADIX ) print_hex_digits( n / RADIX ) ;
    std::cout << digit ;
}

void print_hex( unsigned int n )
{
    std::cout << "0x" ;
    print_hex_digits(n) ;
    std::cout << '\n' ;
}


int main()
{
    unsigned int numbers[] = { 0, 4095, 3058, 405, 1315, 3586, 3584, 12345678, 2882400018 } ; 
    for( unsigned int n : numbers ) print_hex(n) ;
}

http://coliru.stacked-crooked.com/a/b0731ab4cd5eef35
Topic archived. No new replies allowed.