zoukankan      html  css  js  c++  java
  • 04 Windows编程——Unicode

    VS 2017下源码

     1 #include<stdio.h>
     2 
     3 int main()
     4 {
     5     char ASC_a = 'a';
     6     char *ASC_str = "hello";
     7     wchar_t W_a = 'a';
     8     wchar_t *W_str = "hello";
     9     printf("ASC_a = %c
    ", ASC_a);
    10     printf("sizeof(ASC_a) = %d
    ", sizeof(ASC_a));
    11     printf("ASC_str = %s
    ", ASC_str);
    12     /* 打印ASC_a的内存 */
    13     printf("ASC_a = 0x%x
    ", &ASC_a);
    14 
    15     printf("W_a = %c
    ", W_a);
    16     printf("sizeof(W_a) = %d
    ", sizeof(W_a));
    17     printf("W_str = %s
    ", W_str);
    18     /* 打印W_a的内存 */
    19     printf("W_a = 0x%x,0x%x
    ", *((char*)&W_a), *(((char*)&W_a)+1));
    20 
    21     getchar();
    22     return 0;
    23 }
    View Code

    运行结果

    wchar_t *W_str = "hello"; 

    对于宽字符串,如果使用printf("W_str = %s ", W_str);可能不会打印出完整的字符串,而只显示第一个字符。

    其原因是因为:宽字符占用2Byte,第二个字节就是。printf以为自己到头了,所以只打印一个字节。对printf使用%S参数,或者使用wprintf来避免这一问题。我手上的是VS 2017,使用printf("W_str = %s ", W_str);居然也可以正常打印,其他版本的VS估计不行。

    如果是其他版本的VS,可能需要在宽字符前面加L,打印宽字符串需要使用%S。这时候源码如下

     1 #include<stdio.h>
     2 
     3 int main()
     4 {
     5     char ASC_a = 'a';
     6     char *ASC_str = "hello";
     7     wchar_t W_a = L'a';
     8     wchar_t *W_str = L"hello";
     9     printf("ASC_a = %c
    ", ASC_a);
    10     printf("sizeof(ASC_a) = %d
    ", sizeof(ASC_a));
    11     printf("ASC_str = %s
    ", ASC_str);
    12     /* 打印ASC_a的内存 */
    13     printf("ASC_a = 0x%x
    ", &ASC_a);
    14 
    15     printf("W_a = %c
    ", W_a);
    16     printf("sizeof(W_a) = %d
    ", sizeof(W_a));
    17     printf("W_str = %S
    ", W_str);
    18     /* 打印W_a的内存 */
    19     printf("W_a = 0x%x,0x%x
    ", *((char*)&W_a), *(((char*)&W_a)+1));
    20 
    21     getchar();
    22     return 0;
    23 }
    View Code

    还可以使用TCHAR无脑处理Unicode ASCII。如果定义了_UNICODE,那么TCHAR = wchar_t,否则位char。如果定义了_UNICODE,那么_TEXT=L,否则_TEXT=空 

     1 #include<tchar.h>
     2 
     3 int main()
     4 {
     5     TCHAR ASC_a = 'a';
     6     TCHAR *ASC_str = "hello";
     7     TCHAR W_a = L'a';
     8     TCHAR *W_str = L"hello";
     9     _tprintf(_TEXT("ASC_a = %c
    "), ASC_a);
    10     _tprintf(_TEXT("sizeof(ASC_a) = %d
    "), sizeof(ASC_a));
    11     _tprintf(_TEXT("ASC_str = %s
    "), ASC_str);
    12     /* 打印ASC_a的内存 */
    13     _tprintf(_TEXT("ASC_a = 0x%x
    "), &ASC_a);
    14 
    15     _tprintf(_TEXT("W_a = %c
    "), W_a);
    16     _tprintf(_TEXT("sizeof(W_a) = %d
    "), sizeof(W_a));
    17     _tprintf(_TEXT("W_str = %s
    "), W_str);
    18     /* 打印W_a的内存 */
    19     _tprintf(_TEXT("W_a = 0x%x,0x%x
    "), *((char*)&W_a), *(((char*)&W_a)+1));
    20 
    21     _gettchar();
    22     return 0;
    23 }
    View Code

     如果包含了Windows.h,就不用写_TEXT了,使用TEXT

  • 相关阅读:
    『笔记』数学数论(八)
    『笔记』BSGS
    『笔记』组合数学(六)
    01 分数规划
    高斯消元
    拉格朗日插值法
    洛谷网课数论
    [IOI2013]robots 机器人
    P3530 [POI2012]FES-Festival
    NOIP 2015 day1
  • 原文地址:https://www.cnblogs.com/kelamoyujuzhen/p/9298591.html
Copyright © 2011-2022 走看看