• 多字节与宽字节转换


    转换一个字符

    #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);
    }
    
  • 相关阅读:
    How Do I Enable Remote Access To MySQL Database Server?
    超越MySQL:三个流行MySQL分支的对比
    自动杀死UNIX僵死的进程 红联Linux门户 中国领先的Linux技术网站 网站导航 Linux企业应用 UniX技术文章
    公司简介_关于我们_知阅网——免费借阅,送还上门
    超越MySQL:三个流行MySQL分支的对比
    reviewboard升级
    Implementing Regular Expressions
    乐维UP IT领域的社交问答
    Price Comparison Script
    Kill the undesired UNIX processes in one go
  • 原文地址:https://www.cnblogs.com/cfans1993/p/6404016.html
Copyright © 2020-2023  润新知