Char to asciii

Hello, I have problems getting this convertion, I already did it, int (ascii) to char, but I can't reverse that.

This is my code:

1
2
3
4
5
6
7
8
  cout<<"\nTotal Cadena: "<<total;
    for(unsigned int i=0;i<total.size();i++)
    {
        char caracter=total[i];
        cout<<"\nCaracter: "<<caracter<<", ASCII: "<<(int)caracter;
        int ascii=static_cast<int>(caracter);
        cout<<"\nASCII: "<<ascii;
    }


Where: total is a string with all the caracters.

And I get this on the console:

Total Cadena: ¢╦µñÖÇa
Caracter: ¢, ASCII: -67
ASCII: -67
Caracter: ╦, ASCII: -53
ASCII: -53
Caracter: µ, ASCII: -26
ASCII: -26
Caracter: ñ, ASCII: -92
ASCII: -92
Caracter: Ö, ASCII: -103
ASCII: -103
Caracter: Ç, ASCII: -128
ASCII: -128

What am i doing wrong?
Thx
What type of variable is total, how was it defined?

Remember a char can only hold values between -128 to +127 anything else is out of range. Those "special" characters are probably out of the range of the char type.

Also don't forget only char values with values of zero to 127 are valid ASCII characters.
Last edited on
Try line 4:
 
        unsigned char caracter = total[i];
Yep, I have to use unsigned
Thanks
Topic archived. No new replies allowed.