Unexpected results when looking at ASCII codes in C++.

Posted by Columbo on Stack Overflow See other posts from Stack Overflow or by Columbo
Published on 2011-01-11T11:27:44Z Indexed on 2011/01/11 11:54 UTC
Read the original article Hit count: 220

Filed under:
|

Hello,

The bit of code below is extracting ASCII codes from characters. When I convert characters in the normal ASCII region I get the value I expect. When I convert £ and € from the extened region I get a load of 1's padding the INT that I'm storing the character in.

e.g. the output of the below is:

45 (ascii E as expected) FFFFFF80 (extended ascii € as expected but padded with ones)

It's not causing me an issue but I'm just wondering why this happens. Here's the code...

unsigned int asciichar[3];
    string cTextToEncode = "E€";
    for (unsigned int i = 0; i < cTextToEncode.length(); i++)
    {
        asciichar[i] = (unsigned int)cTextToEncode[i];
        cout << hex << asciichar[i] << "\n";    
    }

Can anyone explain why this is? Thanks

© Stack Overflow or respective owner

Related posts about c++

Related posts about ascii