1. 程式人生 > >Linux 多位元組與寬字元的相互轉化(類似MultiByteToWideChar和WideCharToMultiByte)

Linux 多位元組與寬字元的相互轉化(類似MultiByteToWideChar和WideCharToMultiByte)

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下轉碼的資料,這裡比較全: