_tcschrs,_tcslen,WideCharToMultiByte,MultiByteToWideChar,mbstowcs,wcstombs字符串操作

1, _tcslen

说明:
求Unicode字符串的长度,使用跟非Unicode的strlen一样 
参数:
Unicode字符串
返回值:
该Unicode字符串长度
 
2, _tcschrs

_tcschr(str , c);
Find a character in a string, using the current locale or aspecified LC_CTYPE conversion state category.
从一个字符串中查找字符。
例如:
CString str1 = "ABCD#EFGH";
CString str2 = _tcschr(str1,TEXT('#'));
str2 结果为"#EFGH".
str :Null-terminated source string.
c :Characterto be located.
Returns a pointer to the first occurrence of c in str, or NULL ifc is not found.
返回str中的第一个字符c的位置开始的字符串的指针,如果没有发现返回NULL。
while(*lp !=0)
{
cout<<lp;
lp=_tcschr(lp , 0);
}
 
 
 
 
 
 
原型:_INTRIMP wchar_t *wcscat(
wchar_t *strDestination, //'\0'结尾的目标字符串
const wchar_t *strSource //'\0'结尾的源字符串
);
用法:#include <stdlib.h>
功能:把strSource所指字符串添加到strDestination结尾处,覆盖strDestination结尾处的'\0'并添加'\0'。
说明:strSource和strDestination所指内存区域不可以重叠且strDestination必须有足够的空间来容纳strSource的字符串。
返回值 : 返回指向strDestination的指针. No return value is reserved to indicate an error.
备注 : 因为wcscat在strDestination追加strSource前不进行检查,这是一个缓冲区溢出的潜在原因。故使用时应注意。推荐使用wcscat_s替代.
 
 
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; 
}
 
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值