开发者

is it possible to put two kind of character sets in the same file

I just asked this question out of curiousity. Generally as for as I am concerning files are stored with single character set. But where will be the the character set type saved? And is it possible to put two kind of strings(like std::string, std::wstring) in开发者_Go百科 the same file?


Character sets were introduced to allow different programs to interpret the same set of characters (namely, single-byte characters whose decimal value is over 127, or, to put it another way, whose high-order bit is set) in different ways. If you want to switch character sets part way through a file or stream, you would have to signal your program in some way, either in the file or out of band.

As to mixing std::string and std::wstring, while it is possible, it would be, at best, confusing. strings are (generally) ASCII and wstrings are Unicode. When generating your file, you could put a signal or marker in that would tell your program to switch when reading it back in.

Generally, if you need more than one character set, you should be using Unicode (which can be represented with std::wstring). In fact, if you're handling user input at all, you should be using Unicode.

Go read Joel Spolsky's The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!). It should help to make things more clear.


If your question is about the encoding of the source file itself, the answer is that the C++ standard requires an implementation to support source files encoded in the base character set. Complier implementations may support additional character sets. Consult your compiler manuals for more information.

About using std::string and std::wstring variables in the same file, yes you can use the, together.


Character encodings are totally free. Files are containers of bytes. You can encode text into a mixture of ASCII, UTF8, Big5, ... encoded characters, but it's up to you to tell how each is to be interpreted.

The convention, though, is to put a marker at the first position of the file, that denotes the encoding. (cfr Byte Order Mark on wikipedia).

When using xml, this has become far more explicit (yet not completely covered): the encoding has to be on the first line, and that line has to be in utf8. (If the encoding is omitted, it means: "utf-8")


A file is just a sequence of bytes. A byte is just a 8-digit (on any modern hardware) binary number, ranging from 0 to 255 if interpreted as unsigned, or from -128 to 127 if interpreted as signed.

What these bytes mean it up to whoever is designed that particular file format. It could contain a sequence of characters encoded with some single encoding indicated in some way or specified in the file format documentation, it can contain an unholy mess of different encodings with no ways to distinguish between them (I've seen such things in real, critical applications), it can contain a mix of binary and text data, or it can contain binary data that has nothing to do with any characters or character sets whatsoever.

However, if your file format isn't binary, that is, if it contains text and only text, then it's generally an extremely bad idea to mix character sets. Using something uniform and ASCII-compatible like UTF-8 is probably the best way. Even in a binary format, it is still a good idea to encode all the text data in the same encoding. UTF-8 or UTF-16 (or even UTF-32) seem to be good choices there. Sometimes there are different requirements which you have to deal with, though. For example, a binary format may have the "old" version of header and the "new" one. The old one may be using some legacy character set, and the new one may be using some Unicode. That's fine. But when it comes to pure-text formats, I've yet to see a widely used format that allows to mix character sets. Some allow you to choose a single character set for each file, and to put a marker somewhere (like XML, HTML, Python sources).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜