vs_u8前缀

1、ZC: 个人测试下来,VS2015开始 支持 u8前缀。

2、What's New for Visual C++ in Visual Studio 2015  https://msdn.microsoft.com/zh-cn/library/hh409293.aspx

新字符类型和 Unicode 文本 现在支持 UTF-8、UTF-16 和 UTF-32 中的字符文本和字符串,并引入了新字符类型 char16_t 和 char32_t。 字符文本可以具有前缀 u8 (UTF-8)、u (UTF-16) 或 U (UTF-32)(如 U'a),而字符串还可以使用原始字符串等效项 u8R(UTF-8 原始字符串)、uR(UTF-16 原始字符串)或 UR(UTF-32 原始字符串)作为前缀。 通用字符名可以在 unicode 文本中随意使用(如 u'u00EF'、u8"u00EF is i" 和 u"U000000ef is I")。 (C++11)

3、测试

  3.1、vs2015x86 Enterprise Update1  (Thinkpad E40 用vs2015,确实比较卡了...)

#include "stdafx.h"
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;

int main()
{
    //char* pc = u8"AA";
    //u8"";
    char* pc = "退出";
    printf("%s
", pc);
    char *p = pc;
    while (p[0] != 0)
    {
        printf("%02X ", ((int)p[0])&0xFF);
        p ++;
    }
    printf("

");

    std::string str = u8"退出";
    printf("%s
", str.c_str());
    for (int i = 0; i < str.length(); i++)
    {
        char cc = str.at(i);
        printf("%02X ", ((int)cc) & 0xFF);
    }
    printf("
");
    cout << str << endl;

    ::MessageBoxA(0, pc, str.c_str(), 0);
    system("pause");
    return 0;
}

    3.1.1、运行结果输出

    3.1.2、疑问:虽然 编码是对的(参看下面的java代码),但是为何显示是乱码??(printf 和 cout 的问题??)

  3.2、java测试代码:

package str;

public class TstrTest
{
    public static void main(String[] args) throws Exception
    {
        String str = "退出";
        
        byte[] bytesGBK = str.getBytes("gbk");
        for (int i=0; i<bytesGBK.length; i++)
            System.out.print(String.format("%02X ", bytesGBK[i]));
        System.out.println();
        
        byte[] bytesUTF8 = str.getBytes("utf-8");
        for (int i=0; i<bytesUTF8.length; i++)
            System.out.print(String.format("%02X ", bytesUTF8[i]));
    }
}

    3.2.1、运行输出:

4、

5、

原文地址:https://www.cnblogs.com/cppskill/p/6068397.html