Problems converting integer to string (stringstream) c ++

0
#include <iostream>
#include <string.h>
#include <sstream>
using namespace std;

int main(int argc, char *argv[]){

    int n, aux;
    string hexa="";
    stringstream hexa_aux;

    cin >> n;
    while(n != 0){
        aux = n % 16;
        n/=16;
        switch (aux){
            case 10:
                hexa+="A";
                break;

            case 11:
                hexa+="B";
                break;

            case 12:
                hexa+="C";
                break;

            case 13:
                hexa+="D";
                break;

            case 14:
                hexa+="E";
                break;

            case 15:
                hexa+="F";
                break;

            default:
                hexa_aux << aux;
                hexa+=hexa_aux.str();
                break;
        }
    }
    for(int i = hexa.length()-1; i>=0; i--){
        cout << hexa[i];
    }
    return 0;
}

I'm having trouble converting an integer to string , when the input is 36, the right result should be 24, but the value ends up going out 244, among other values.

Is the problem with conversion?

    
asked by anonymous 10.08.2017 / 02:13

2 answers

0

The error is indeed in this line:

hexa+=hexa_aux.str();

You should only assign, not concatenate, like this:

hexa=hexa_aux.str();

That will make the program work. However this can be simplified by using the ASCII table to get to the characters that interest, as well as reverse printing:

int main(int argc, char *argv[]){

    int n, aux;
    stringstream hexa_aux;

    cin >> n;

    while(n != 0){
        aux = n % 16;
        n/=16;

        if (aux >9){ //concatenar as letras
            hexa_aux << (char)(aux + 'A'- 10);
        }
        else { //concatenar os números
            hexa_aux << (char)(aux + '0');
        }
    }

    string aux_str = hexa_aux.str();
    cout<<string(aux_str.rbegin(), aux_str.rend()); //imprimir invertido

    return 0;
}

To better understand the logic applied to character building, you need to look at the ASCII table . If we look at the part of the numbers we see:

48  '0' zero
49  '1' um
50  '2' dois
51  '3' três
52  '4' quatro
53  '5' cinco
54  '6' seis
55  '7' sete
56  '8' oito
57  '9' nove

Where the first column is the integer value of each letter. So if we have the number 0 and we want the corresponding letter just add 48 we get with '0' . From 1 to 49 ( '1' ) just add 48 . E 48 is exactly the '0' value. Logo

num + '0'

Or

num + 48 

Reaches the character you want. What explains this part:

hexa_aux << (char)(aux + '0');

For the part of A to F is the same principle but we can not directly add 'A' because the number we have to transform into A is 10 , and so we would stop the 10th letter of the alphabet. In this case it is necessary to deduct this bit by removing 10 :

hexa_aux << (char)(aux + 'A'- 10);

See it working on Ideone

    
10.08.2017 / 03:01
0

Your problem is that stringstream hexa_aux needs to be reset at every switch interaction. She is remembering the previous values. The easiest way to resolve this is to declare it in the scope of the default option. That is,

default:
     stringstream hexa_aux;  // Declare aqui, assim ela se reinicializa
                             // a cada chamada do switch
     hexa_aux << aux;
     hexa+=hexa_aux.str();
     break;

A second alternative is to forget about stringstream and use the std::to_string() function. Example:

default:
     hexa+=std::to_string(aux);
     break;
    
29.08.2017 / 16:44