[MFC]CString转char[]

来源:互联网 发布:alien skin x2 mac 编辑:程序博客网 时间:2024/05/22 00:39
/*
 * 函数名: CString2Char
 * 参数1: CString str                     待转换字符串
 * 参数2: char ch[]                       转换后将要储存的位置
 * 将Unicode下的CString转换为char*
 */
voidCString2Char(CString str,charch[])
{
    int i;
    char*tmpch;
    intwLen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);//得到Char的长度
    tmpch   =newchar[wLen + 1];                                            //分配变量的地址大小
    WideCharToMultiByte(CP_ACP, 0, str, -1, tmpch, wLen, NULL, NULL);      //将CString转换成char*
     
    for(i = 0; tmpch[i] !='\0'; i++) ch[i] = tmpch[i];
    ch[i] ='\0';
}
0 0