linux沒有WideCharToMultiByte,MultiByteToWideChar,我們通常…

來源:互聯網
上載者:User

Linux下面的沒有命名為 WideCharToMultiByte() 和 MultiByteToWideChar() 函數,WideCharToMultiByte,MultiByteToWideChar是windows下的函數,在linux下也有類似的兩個函數:

mbstowcs()
wcstombs()

值得注意的是:

size_t mbstowcs(wchar_t *wcstr,const char *mbstr,size_t count);

這個函數的第三個參數count,大小一定要是mbstr長度的2倍,否則出來的中文也會是亂碼

 

測試一下:

 

    setlocale(LC_ALL,"zh_CN.GB18030");
    wchar_t wcstr[20] = L"字元測試123abc";

    int len = wcslen(wcstr)+1;
    printf("len = %d /n",len);
    for(int i = 0; i < len; i++)
        printf("0x%08x ",wcstr[i]);
    printf("/n");

    char str[55] = {0};   
    int n = wcstombs(str,wcstr,55);
    if(-1 == n)
    {
    perror("wcstombs ");
    exit(-1);
    }   
    printf("n = %d/n",n);
    for(int i = 0; i < n+1; i++)
        printf("0x%08x ",str[i]);
    printf("/n");   
    wchar_t wch[50]={0};
    int m = mbstowcs(wch,str,n);    
    if(m == -1)
    {
    perror("Converting");
    exit(-1);
    }
    printf("m = %d/n",m);
    for(int i =0; i<m+1;i++)
    printf("0x%08x ",wch[i]);
    printf("/n");
    return 0;
}

 

還有呢,轉碼還可以使iconv函數族,包含以下三個函數:
iconv_t iconv_open(const char *tocode, const char *fromcode);

size_t iconv(iconv_t cd,char **inbuf,size_t *inbytesleft,char **outbuf,size_t *outbytesleft);

int iconv_close(iconv_t cd);

 

 

測試一下:

#include <stdio.h>
#include <iconv.h>
#include <string.h>

#define BUFLEN 256

char outbuf[BUFLEN];
char inbuf[BUFLEN] = "characters convertion";

int main()
{    
     char *pin = inbuf;
     char *pout = outbuf;
       
     int inlen = strlen(pin);
     int outlen = BUFLEN;

     int retsize;

     iconv_t cd;

     cd = iconv_open("UTF-8", "GBK");

     if((iconv_t)-1 == cd) {
             perror("iconv_open error");
             return -1;
     }

     retsize = iconv(cd, &pin, (size_t *)&inlen, &pout, (size_t *)&outlen);

     if((size_t)-1 == retsize) {
             perror("iconv error");
             return -2;   
     }

     if(outlen > 0) {
             printf("%s/n", outbuf);
     }

     iconv_close(cd);

     return 0;
}

 

另外

 

關於Linux下轉碼的資料,這裡比較全:

 

http://www.360doc.com/content/11/0119/14/1317564_87612492.shtml

 

仔細讀,所有的問題基本都可以找到答案。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.