extended ascii and istream& getline (char* s, ...

Jul 10, 2012 at 12:46pm
Hi all,
If I want to deal with extended ascii, i.e. from 128 to 255, shall I use char or unsigned char ? If I shall use unsigned char, how to make it compatible with standard libs functions like stream& getline (char* s, ...) ?
Jul 10, 2012 at 1:32pm
Is there anything like unsigned char?

cout<<(char)<code>

Thats it.
Jul 10, 2012 at 2:30pm
If you had an unsigned char array you could just cast it inside the call to getline: getline((char*)uc_array,size);
Jul 10, 2012 at 3:00pm
Pretty sure your unsigned char array will work just fine.
Jul 10, 2012 at 3:15pm
Thanks a lot ! That said, it would have been more logical if the standard library work with unsigned char, including std::string. It is like if the standard does not recognize the extended ascii. I perform algorithmic on extended ascii, and it would be a nightmare to use signed char, thus char.
Jul 10, 2012 at 3:22pm
The standard library recognizes extended ascii just fine, as long as the appropriate locale is installed. The storage type is char/string.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cctype>
#include <clocale>
 
int main()
{
    const char c = '\xdf'; // German letter ß in ISO-8859-1
 
    std::cout << "isalnum(\'\\xdf\', default C locale) returned "
               << std::boolalpha << (bool)std::isalnum(c) << '\n';
 
    std::setlocale(LC_ALL, "de_DE.iso88591");
    std::cout << "isalnum(\'\\xdf\', ISO-8859-1 locale) returned "
              << std::boolalpha << (bool)std::isalnum(c) << '\n';
 
}
isalnum('\xdf', default C locale) returned false
isalnum('\xdf', ISO-8859-1 locale) returned true
Last edited on Jul 10, 2012 at 3:24pm
Jul 10, 2012 at 8:06pm
Great. Thank you very much Cubbi. This is not directly linked to my original problem, but still very usefull since in my next project steps, you prevent me here from having big headackes.
My use case is char arithmetics for text hashing. Manipulating unsigned char is simpler.
Topic archived. No new replies allowed.