设为首页 加入收藏

TOP

c++编写字符串编码类(二)
2012-11-13 13:24:28 来源: 作者: 【 】 浏览:781
Tags:编写 字符串 编码

 

    void CEncodeString::LoadString(char *str,int mode/* =ansi */)

    {

    if(mode==ansi)

    {

    string strTemp;

    strTemp+=str[0];

    strTemp+=str ;

    if(strTemp==GetUnicodeHeader())

    {

    LoadString((WCHAR*)(str+2));

    return;

    }

    strTemp+=str ;

    if(strTemp==GetUtf8Header())

    {

    str+=3;

    mode=utf8;

    }

    else

    {

    m_str=str;

    }

    }

    if(mode==utf8)

    {

    int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, str, strlen(str), NULL, 0);

    wchar_t* wszString = new wchar_t[wcsLen + 1];

    ::MultiByteToWideChar(CP_UTF8, NULL, str, strlen(str), wszString, wcsLen);

    wszString[wcsLen] = '\0';

    int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL);

    char* szAnsi = new char[ansiLen + 1];

    ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL);

    szAnsi[ansiLen] = '\0';

    m_str=szAnsi;

    delete[] wszString;

    delete[] szAnsi;

    }

    }

    void CEncodeString::LoadString(WCHAR *wStr)

    {

    int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wStr, wcslen(wStr), NULL, 0, NULL, NULL);

    char* szAnsi = new char[ansiLen + 1];

    ::WideCharToMultiByte(CP_ACP, NULL, wStr, wcslen(wStr), szAnsi, ansiLen, NULL, NULL);

    szAnsi[ansiLen] = '\0';

    m_str=szAnsi;

    delete[] szAnsi;

    }

    wstring CEncodeString::ToUnicode()

    {

    char* szAnsi =(char*)m_str.data();

    int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), NULL, 0);

    wchar_t* wszString = new wchar_t[wcsLen + 1];

    ::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), wszString, wcsLen);

    wszString[wcsLen] = '\0';

    wstring wStr=wszString;

    delete[] wszString;

    return wStr;

    }

    string CEncodeString::ToAnsi()

    {

    return m_str;

    }

    string CEncodeString::ToUtf8()

    {

    wstring wStr=ToUnicode();

    wchar_t* wszString =(wchar_t*) wStr.data();

    int u8Len = ::WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL);

    char* szU8 = new char[u8Len + 1];

    ::WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), szU8, u8Len, NULL, NULL);

    szU8[u8Len] = '\0';

    string strUtf8=szU8;

    delete[] szU8;

    return strUtf8;

    }

    string CEncodeString::GetUnicodeHeader()

    {

    return "\xff\xfe";

    }

    string CEncodeString::GetUtf8Header()

    {

    return "\xef\xbb\xbf";

    }

    main()

    {

    setlocale(LC_CTYPE, "");

    FILE *f=fopen("d:\\json.txt","rb");

    char buf[100]={0};

    fread(buf,1,100,f);

    fclose(f);

    CEncodeString encode;

    encode.LoadString(buf);

    cout《encode.ToAnsi()《endl;

    }

    这个类会自动解析字符串的头信息,判断是否为unicode还是utf8还是ansi,然后会做相应的处理,当然字符串在类中的储存方式都是ansi的。

    本文由不足之处,还望大家多多指正。

      

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇练习c++ 函数模版例子 下一篇矩阵链乘法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: