本系列文章由 @YhL_Leo 出品,转载请注明出处。

文章链接: http://blog.csdn.net/yhl_leo/article/details/49592361


编程时遇到BUG:error C2440: cannot convert from 'const char [7]' to 'LPCWSTR',查找后发现错误原因之一竟然是字符串的字符集编码模式造成。

所有的string 类都是以C-style字符串为基础的。C-style字符串是字符数组。所以我们先介绍字符类型。这里有3种编码模式对应3种字符类型。

  • 单子节字符集(single-byte character set, SBCS)。在这种编码模式下,所有的字符都只用一个字节表示。ASCII是SBCS。一个字节表示的0用来标志SBCS字符串的结束。

  • 多字节字符集(multi-byte character set, MBCS)。一个MBCS编码包含一些一个字节长的字符,和一些大于一个字节长的字符。用在Windows里的MBCS包含两种字符类型,单字节字符(single-byte characters)和双字节字符(double-byte characters)。由于Windows里使用的多字节字符绝大部分是两个字节长,所以MBCS常被用DBCS(double-byte character set, DBCS)代替。像SBCS一样,DBCS字符串的结束标志也是一个单字节表示的0。

  • 统一的双字符集(Unicode Character Set)。Unicode是一种所有的字符都使用两个字节编码的编码模式。Unicode字符有时也被称作宽字符(Wide Character),因为它比单子节字符宽(使用了更多的存储空间)。注意,Unicode不能被看作MBCS。MBCS的独特之处在于它的字符使用不同长度的字节编码。Unicode字符串使用两个字节表示的0作为它的结束标志。

单字节字符包含拉丁文字母表,accented characters及ASCII标准和DOS操作系统定义的图形字符。双字节字符被用来表示东亚及中东的语言。Unicode被用在COM及Windows NT操作系统内部。

你一定已经很熟悉单字节字符。当你使用char时,你处理的是单字节字符。双字节字符也用char类型来进行操作(这是我们将会看到的关于双子节字符的很多奇怪的地方之一)。Unicode字符用wchar_t来表示。Unicode字符和字符串常量用前缀L来表示。

常用的将字符串编码转为统一双字节编码的方法,是使用宏定义_T()

// #include <tchar.h>
// #include <windows.h> #define __T(x) L ## x
#define _T(x) __T(x) _T("ABC"); // "ABC" transforms into wide characters, equals to L"ABC"

其中宏定义的含义,可以阅读“#”的作用和用法一文。

在Microsoft Visual Studio中,project -> properties -> Configuration Properties -> General -> Character Set中,包含三种模式:

  • Not Set
  • Use Unicode Character Set
  • Use Multi-Byte Character Set

回到BUG本身,在使用赋值语句将字符串的值赋予LPCWSTR类型时,如果使用_T(.)转换编译失败,那么除去头文件包含的问题,那么很有可能就是Character Set模式设置的问题。

Multi-Byte Character Set & Unicode Character Set的更多相关文章

  1. Unicode Character Set and UTF-8, UTF-16, UTF-32 Encoding

    在计算机内存中,统一使用unicode编码,当需要保存到硬盘或者需要传输的时候,就转换为utf-8编码. 用记事本编辑的时候,从文件读取的utf-8字符被转换为unicode字符到内存里,编码完成保存 ...

  2. Unicode Character Table – Unicode 字符大全

    Unicode(统一码.万国码.单一码)是一种在计算机上使用的字符编码.它为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言.跨平台进行文本转换.处理的要求.Unicode Chara ...

  3. nginx启动报错(1113: No mapping for the Unicode character exists in the target multi-byte code page)

    使用windows版本的nginx启动时遇到(1113: No mapping for the Unicode character exists in the target multi-byte co ...

  4. Java的IO操作中有面向字节(Byte)和面向字符(Character)两种方式

    解析:Java的IO操作中有面向字节(Byte)和面向字符(Character)两种方式.面向字节的操作为以8位为单位对二进制的数据进行操作,对数据不进行转换,这些类都是InputStream和Out ...

  5. failed (1113: No mapping for the Unicode character exists in the target multi-byte code page), client: 127.0.0.1...

    nginx部署网站后,访问域名,网页显示  500 Internal Server Error ,经查看发现nginx的error.log中有报错: failed (1113: No mapping ...

  6. nginx 启动报错 1113: No mapping for the Unicode character exists in the target multi-byte code

    failed (1113: No mapping for the Unicode character exists in the target multi-byte code page) 因为路径有中 ...

  7. Windows版Nginx启动失败之1113: No mapping for the Unicode character exists in the target multi-byte code page

    Windows版Nginx启动一闪,进程中未发现nginx进程,查看nginx日志,提示错误为1113: No mapping for the Unicode character exists in ...

  8. IIS7的FTP出错: 451 No mapping for the unicode character exists in the target multi-byte code page

    提示:IIS7的FTP出错: 451 No mapping for the unicode character exists in the target multi-byte code page 今天 ...

  9. Java基础知识强化68:基本类型包装类之Character概述和Character常见方法

    1. Character概述: public final class Character extends Object implements Serializable,Comparable<Ch ...

随机推荐

  1. [NOIPlus]斗地主

    毫无意义的一道题. 用pai[i]表示某种点数的牌的剩余量,used[i]表示单,对,三,四的出牌数,大力分类讨论,大力dfs即可...真奇葩... #include <iostream> ...

  2. [转载] Linux新手必看:浅谈如何学习linux

    本文转自 https://www.cnblogs.com/evilqliang/p/6247496.html 本文在Creative Commons许可证下发布 一.起步 首先,应该为自己创造一个学习 ...

  3. java实现组合数_n!_杨辉三角_组合数递推公式_回文数_汉诺塔问题

    一,使用计算机计算组合数 1,设计思想 (1)使用组合数公式利用n!来计算Cn^k=n!/k!(n-k)!用递推计算阶乘 (2)使用递推的方法用杨辉三角计算Cn+1^k=Cn^k-1+Cn^k 通过数 ...

  4. elementUI 日期时间选择器el-date-picker开始时间与结束时间约束

    主要思路:el-date-picker组件需要 :picker-options属性,属性值为data,data的数据来自于methods中的方法. ##template代码 <el-form-i ...

  5. ASP.NET-Router配置中MapRoute的参数

    App_Start文件夹中的RouteConfig MapRoute(string name,string url); MapRoute(string name,string url,object d ...

  6. npm --save 选项作用

    npm --save 选项作用 学习了:https://segmentfault.com/q/1010000000403629 -save和save-dev可以省掉你手动修改package.json文 ...

  7. [HTML5] Inlining images with SVG and data URIs

    The main reason you want to do I"nlining images with SVG and data URIs" is to reduce http ...

  8. LeetCode211:Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  9. struct和typedef

    struct Test { int i; }; 解析:此处声明一个Test的结构体. 使用:在C语言中:struct Test t(此处的struct不可省略),在C++中:Test t(能够省略st ...

  10. Oracle 实现 mysql 更新 update limit

    oracle给人的感觉非常落后.使用非常不方便,Toad 这个软件又笨又迟钝.pl/sql更是,90年代的界面风格,速度还卡得要死.并且oracle不支持limit .by default7#zbph ...