zoukankan      html  css  js  c++  java
  • 多字节与宽字节转换

    转换一个字符

    #include <stdlib.h>
    int mblen(const char *s,size_t n);
    int mbtowc(wchar_t *pwc,cosnt char *s,size_t n);
    int wctomb(char *s,wchar_t wchar);
    

    mblen用于检测s指针位置第一个字符占有的字节数,n为要检测的字节数,一般指定为MB_CUR_MAX mblen返回值:如果s指向汉字返回2,s指定英文字符返回1 mbtowc将一个字符从多字节编码转到宽字节编码 wctomb将一个字符从宽字节编码转到多字节编码

    #include <stdio.h>
    #include <string.h>
    #include <locale.h>
    #include <stdlib.h>
     
    int main(int argc,char *argv[]){
        setlocale( LC_ALL, "chinese-simplified" );
     
        char *str = "中国中国abc";
        int size = strlen (str);
        printf ("字节数:%d
    ", size);
        printf("中的gbk编码为:0x%x%x
    ",(unsigned char)str[0],(unsigned char)str[1]);
     
        //mbtowc
        size = mblen (str, MB_CUR_MAX); //此时为2
        //size=mblen(&str[8],MB_CUR_MAX); //此时为1
        printf("size=%d
    ",size);
        wchar_t w[2]={0};
        mbtowc(w,str,size);
        wprintf(L"%s
    ",w);
        printf("中的unicode编码为:0x%x
    ",w[0]);
     
        //wctomb
        char sav[3]={0};
        wctomb(sav,w[0]);
        printf("sav=%s
    ",sav);
        return(0);
    }
    

    转换字符串

    int mbstowcs(wchar_t *pwcs,const char *s,size_t n);
    int wcstombs(char *s,const wchar_t *pwcs,size_t n);
    

    n的作用者: 在mbstowcs中最多生成n个wchar_t对象并保存到pwcs指定的宽字符数组中; 在wcstombs中最多生成n个char保存到s指定的多字节字符数组中

    #include <stdio.h>
    #include <string.h>
    #include <locale.h>
    #include <stdlib.h>
     
    int main(int argc,char *argv[]){
        setlocale( LC_ALL, "chinese-simplified" );
     
        char *str = "中国a中国bc";
        wchar_t w[10]={0};
        mbstowcs(w,str,7); //要生成7个wchar_t,而str中刚好有7个完整的字符
        wprintf(L"w=%s
    ",w); //与str内容相同,全部输出
     
        char sav[10]={0};
        wcstombs(sav,w,6);//要生成6个char,中国占4个char,a占一个char,还有一个char因不能完整转换"中"字,所以被丢弃,只转换了5个字符
        printf("sav=%s
    ",sav); //只输出"中国a"
        return(0);
    }
    
  • 相关阅读:
    zlog 使用手册
    Contribution and Coding Style Guide Bullet Physics Library
    Windows系统读写ext2/3/4文件系统的工具「ext2fsd」
    blender svn
    c# 各个版本
    c# Looping with the foreach statement
    C# 9.0 and .NET 5 – Modern Cross-Platform Development
    我的常用博客
    qml通过stacklayout实现页面切换
    JSON解析-android平台(转)
  • 原文地址:https://www.cnblogs.com/cfans1993/p/6404016.html
Copyright © 2011-2022 走看看