格式化輸出中的%s和%S的區別

來源:互聯網
上載者:User

請看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為中文字元,中文字元一般會佔滿兩位元組,而按單位元組字元就會按一個位元組一個位元組的輸出,故會輸出亂碼。

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.