字符串格化中%s与%S的区别 您所在的位置:网站首页 牙膏a和s有什么区别 字符串格化中%s与%S的区别

字符串格化中%s与%S的区别

2024-07-06 08:07| 来源: 网络整理| 查看: 265

我们先来看一下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针对宽字符,wprintf针对单字符。 CString中的format与printf类似,在unicode字符集的工程中:

CString str1, str2; str1.format(_T("%S"), str2);

%S专指单字节字符的字符串,而str2为宽字符,类型不匹配,故出现不可预期的错误。

若str2为英文字符,如“abcd”,就只能输出a,因str2为宽字符,a有两个字节,值为0x0061,在内存中为61 00,故按单字节输出只能输出61,碰到00,即空字符后认为字符串结束,不会再输出。 若str2为中文字符,中文字符一般会占满两字节,而按单字节字符就会按一个字节一个字节的输出,故会输出乱码。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有