Use Unsigned String?

I

Immortal Nephi

Do string support extended ASCII (128 through 255)? Can I use typedef
like this below.

typedef basic_string<unsigned char, char_traits<unsigned char>,
allocator<unsigned char> > ustring;

Why do C++ Compiler fail to compile if I do below.

ustring s1 = ""; // Error

I want to put all 256 ASCII codes into full string.
 
R

Rolf Magnus

Immortal said:
Do string support extended ASCII (128 through 255)?

The C++ standard doesn't specify a character set, and you can basically
store anything in std::string, so the answer would be yes.
Can I use typedef like this below.

typedef basic_string<unsigned char, char_traits<unsigned char>,
allocator<unsigned char> > ustring;

I guess you could, but why would you? The type for characters is char, not
unsigned char.
Why do C++ Compiler fail to compile if I do below.

ustring s1 = ""; // Error

Because the type is wrong. Doesn't your compiler tell you that?
What do you think the meaning of an "unsigned string" is?
I want to put all 256 ASCII codes into full string.

There are only 127 ASCII codes. Anything beyond that are other character
sets.
 
J

James Kanze

Do string support extended ASCII (128 through 255)? Can I use
typedef like this below.
typedef basic_string<unsigned char, char_traits<unsigned char>,
allocator<unsigned char> > ustring;

That's undefined behavior. The standard doesn't require an
instantiation of char_traits<unsigned char>, and of the two
compilers I've heard of which do provide one, they do it
differently.
Why do C++ Compiler fail to compile if I do below.
ustring s1 = ""; // Error

Because the type of "" is char const[1] (which converts to char
const*), and your ustring wants an unsigned char const*.
I want to put all 256 ASCII codes into full string.

That's going to be difficult, considering that there are only
128 ASCII codes. But in practice, you can put all 256 of the
ISO 8859-1 characters in an std::string, without too many
problems. (I regularly use std::string for UTF-8, for example.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top