现有UNICODE字符串,“4F60002F7B54002F5BF9002F4E86”
要求写一个函数,返回这个UNICODE字符串对应的ANSI字符~~~
谁先写出这个函数,并且告诉我这些字串是什么意思就把分给他。显然,这时个7个字符的字符串。

解决方案 »

  1.   

    int UnicodeToAnsi(const unsigned char *szSrc, char *szDest, int iSrcLen)
    {
    int nDstLength;        // UNICODE宽字符数目
    WCHAR szTempCont[160] = {'\0'}; // UNICODE串缓冲区 // 高低字节对调,拼成UNICODE
    for (int i = 0; i < iSrcLen / 2; i++)
    {
    szTempCont[i] = *szSrc++ << 8;
    szTempCont[i] |= *szSrc++;
    }
    nDstLength = ::WideCharToMultiByte(CP_ACP,
    0,
    szTempCont,
    iSrcLen / 2,
    szDest, 160, NULL, NULL );
    szDest[nDstLength] = '\0';
    return nDstLength;
    }
      

  2.   

    来晚了,不知道还有没有分了...
    WideCharToMultiByte()
      

  3.   

    你/答/对/了我是来赚分的。VBS写的。Function Uni2Asc(strUni)
    strHex = "0123456789ABCDEF"
    iLen = Len(strUni)
    For i = 1 To iLen Step 4
    iValue = 0
    strTemp = Mid(strUni , i , 4)
    For j = 1 To 4
    chTmp = Mid(strTemp , j , 1)
    iTmp = InStr(strHex , chTmp) - 1
    iValue = iValue*16+iTmp
    next
    strAsc = strAsc + chrw(iValue)
    next
    End Function
    MsgBox(Uni2Asc("4F60002F7B54002F5BF9002F4E86"))
      

  4.   

    按照1楼的做~~
    nDstLength=14,结果也是乱码,明显不对
      

  5.   

    char ustring[] = "4F60002F7B54002F5BF9002F4E86";
    wchar_t* wstring = new wchar_t[sizeof(ustring)/4+1];
    wstring[sizeof(ustring)/4] = L'\0';
    for (int i = 0, j = 0; i < sizeof(ustring)/4;++i)
    {
    int a;
    char temp[4] = {0};
    strncpy(temp, ustring+j, 4);
    sscanf(temp, "%x", &a);
    *(wstring+i) = a; j += 4;
    }

    int nLens = ::WideCharToMultiByte(CP_ACP, 0, wstring, -1, NULL, 0, 0, 0);
    char* astring = new char[nLens+1];
    astring[nLens] = '\0';
    ::WideCharToMultiByte(CP_ACP, 0, wstring, -1, astring, nLens, 0, 0);
      

  6.   

    注意下new的部分,不用时delete,然后赋指针空.
      

  7.   


    wchar_t const szBuffer[] = { L"4F60002F7B54002F5BF9002F4E86" };DWORD dwCount;
    char* szTarget;dwCount = ::WideCharToMultiByte(CP_ACP, NULL, szBuffer, -1, NULL, NULL, NULL, FALSE);
    szTarget = new char[dwCount+1];::WideCharToMultiByte(CP_ACP, NULL, szBuffer, -1, szTarget, dwCount, NULL, FALSE);
      

  8.   

    //Unicode to ANSI
    BOOL UnicodeToANSI( char* des,const TCHAR *src )
    {
            if(NULL == des)
                return FALSE;
    char*     pElementText;
    int    iTextLen;
    // wide char to multi char
    iTextLen = WideCharToMultiByte( CP_ACP,
    0,
    src,
    -1,
    NULL,
    0,
    NULL,
    NULL );
    pElementText = new char[iTextLen + 1];
    memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
    ::WideCharToMultiByte( CP_ACP,
    0,
    src,
    -1,
    pElementText,
    iTextLen,
    NULL,
    NULL );
    strcpy(des,pElementText);
    delete[] pElementText; return TRUE;
    }
      

  9.   

    /* 如果你不再使用这两个值,就注释掉这部分
    delete [] wstring;
    wstring = NULL;
    delete [] astring;
    astring = NULL;
    */
      

  10.   

    #include "stdafx.h"
    #include "windows.h"
    #include "tchar.h"
    #include <iostream>
    using namespace std;char* ChW2A (const wchar_t* su)
    {
    char* saRet = NULL;
    if(su)
    {
    int nStrUnicodeLen = wcslen(su);
    saRet = new char[nStrUnicodeLen*2+1];
    if(saRet)
    {
    memset(saRet, 0, (nStrUnicodeLen*2+1)*sizeof(char));
    WideCharToMultiByte(CP_ACP, 0, su, nStrUnicodeLen, saRet, nStrUnicodeLen*2+1, NULL, NULL);
    }
    }
    return saRet;
    };int main(int argc, char* argv[])
    {
    wchar_t w[] = {0x4F60,0x002F,0x7B54,0x002F,0x5BF9,0x002F,0x4E86,0x00};
    cout << ChW2A(w) <<endl;
    return 0;
    }