請看MSDN:http://msdn.microsoft.com/zh-cn/library/hf4y5e3w(v=vs.90).aspx
的解釋。
s |
String |
When used with printf functions, specifies a single-byte–character string; when used with wprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached. |
S |
String |
When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached. |
使用s時,printf是針對單位元組字元的字串,而wprintf是針對寬字元的
使用S時,正好相反,printf針對寬字元
CString中的format與printf類似,在unicode字元集的工程中,使用
CString str1, str2;
str1.format(_T("%S"), str2);
時
%S專指單位元組字元的字串,而str2為寬字元,類型不符,故出現不可預期的錯誤。
若str2為英文字元,如“abcd”,就只能輸出a,因str2為寬字元,a有兩個位元組,值為0x0061,在記憶體中為61 00,故按單位元組輸出只能輸出61,碰到00,即Null 字元後認為字串結束,不會再輸出。
若str2為中文字元,中文字元一般會佔滿兩位元組,而按單位元組字元就會按一個位元組一個位元組的輸出,故會輸出亂碼。