Problems converting integer to string (stringstream) c++

Asked

Viewed 422 times

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 coming out 244, among other values.

The problem is with conversion?

2 answers

0


The error is indeed in this line:

hexa+=hexa_aux.str();

That you should just assign, not concatenate, like this:

hexa=hexa_aux.str();

That will make the program work. Although this can be simplified, using the ASCII table to get to the characters that matter, as well as in the 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 the construction of the characters, it is necessary to look at the table ASCII. If we look at the numbers part 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 that we get '0'. Of 1 for 49 ('1') just add up 48. And 48 is exactly the value of '0'. Soon

num + '0'

Or

num + 48 

It 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 cannot directly add the 'A' because the number we have to turn into A is the 10, and that is why we would stop the 10th letter of the alphabet. In this case it is necessary to discount this bit removing 10:

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

Watch it work on Ideone

0

Your problem is that the stringstream hexa_aux needs to be reset to each interaction of switch. She’s remembering the previous values. The easiest way to solve it is to declare it in the option scope default. That is to say,

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 the stringstream and use the function std::to_string(). Example:

default:
     hexa+=std::to_string(aux);
     break;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.