用CFile的成员函数读取一个非unicode文件是否能直接得到unicode编码的字符串呢,还是要自己手动转换

解决方案 »

  1.   

    自己转换
    MultiByteToWideChar();
      

  2.   

    必须自己转换。
    //**************************************
    // ansi字符串转unicode字符串
    // 返回大于0成功,小于0失败
    //**************************************
    int
    astr_ustr( char *ansistr, WCHAR *unicodestr, int len )
    {
    int result = 0;
    try
    {
    int needlen;
    if( 0 == len )
    {
    needlen = MultiByteToWideChar( g____ansi_codepage, 0, ansistr, -1, NULL, 0 );
    if( needlen < 0 )
    {
    return needlen;
    }
    result = MultiByteToWideChar( g____ansi_codepage, 0, ansistr, -1, unicodestr, needlen );
    }
    else
    {
    needlen = MultiByteToWideChar( g____ansi_codepage, 0, ansistr, len, NULL, 0 );
    if( needlen < 0 )
    {
    return needlen;
    }
    result = MultiByteToWideChar( g____ansi_codepage, 0, ansistr, len, unicodestr, needlen );
    } if( result < 0 )
    {
    return result;
    }
    return result;
    }
    catch( ... )
    {
    ShowError();
    }
    return result;
    }
      

  3.   

    ANSI 转 UNICODE的一种方法:
        //设置当前地域信息,不设置的话,使用这种方法,中文不会正确显示
        //需要#include<locale.h>
        setlocale(LC_CTYPE, "chs"); 
        wchar_t wcsStr[100];
        //注意下面是大写S,在unicode中,代表后面是ansi字符串
        //swprintf是sprintf的unicode版本
        //格式的前面要加大写L,代表是unicode
        swprintf(wcsStr, L"%S", szAnsi);
      

  4.   

    g____ansi_codepage
    最受不了这种风格,看见就烦!^_^