我想在我的程序中加入Google搜索支持,也就是说,我想给出一个关键词,然后直接打开Google搜索此关键词的页面,我采用的是这样的方法:
加入我想搜索:源代码 下载
我发现手工在Google搜索时,IE地址栏里显示:
http://www.google.com/search?hl=zh-CN&q=%E6%BA%90%E4%BB%A3%E7%A0%81+%E4%B8%8B%E8%BD%BD&btnG=Google%E6%90%9C%E7%B4%A2&lr=在程序中用ShellExecute执行该字符串,可以打开正确的搜索页面,但是若换成:
http://www.google.com/search?hl=zh-CN&q=源代码+下载&btnG=Google%E6%90%9C%E7%B4%A2&lr=就不行了,可是我需要动态设置搜索关键词,但是我不知道如何将“源代码 下载”转换为“E6%BA%90%E4%BB%A3%E7%A0%81+%E4%B8%8B%E8%BD%BD”的形式,怎么办?请高手们指点一下迷津!谢谢!

解决方案 »

  1.   

    很简单,只要
    char c[2] = "中"
    WORD * pw = (WORD)c;
    pw[0]就是内码值,然后转成16进制的字符串形势就可以了记得加%标记
      

  2.   

    好像长度不对呀,“下载”怎么会对应“E4%B8%8B%E8%BD%BD”6个字节呢?
      

  3.   

    如果有现成的转换API就好了,不知道有没有人也碰到过这样的问题?
      

  4.   

    http://community.csdn.net/Expert/topic/3388/3388147.xml?temp=5.057925E-02
    现成的给你
      

  5.   

    多谢 aspvbjava(注定走入地狱),不过正确的解决方案还要结合UTF-8编码才行,我整理了一下代码,贴在下面:(注:以下代码是结合Google搜索的需求而编写的,若要应用于其它场合和能需要做适当的改动)// Code page conversion function
    static int ConvertCodePage(UINT uSrcCodePage, LPCSTR pcszIn, int iLen, UINT uDstCodePage, LPSTR &pszOut)
    {  
    int iWideCharLen = ::MultiByteToWideChar(uSrcCodePage, 0, pcszIn, iLen, NULL, 0);
    LPWSTR lpszWideChar = new WCHAR[iWideCharLen + 1]; memset(lpszWideChar, 0, (iWideCharLen + 1) * sizeof(WCHAR)); iWideCharLen = ::MultiByteToWideChar(uSrcCodePage, 0, pcszIn, iLen, lpszWideChar, iWideCharLen); if (uDstCodePage == 54936 && !::IsValidCodePage(54936))
    {
    uDstCodePage = 936;
    } int iOutLen = ::WideCharToMultiByte(uDstCodePage, 0, lpszWideChar, iWideCharLen, NULL, 0, NULL, NULL); pszOut = new CHAR[iOutLen + 1];
    memset(pszOut, 0, iOutLen + 1); iOutLen = ::WideCharToMultiByte(uDstCodePage, 0, lpszWideChar, iWideCharLen, pszOut, iOutLen, NULL, NULL); delete[] lpszWideChar; return iOutLen;
    }// URL encoding function
    static int EncodeURL(LPCSTR pcszIn, int iLen, LPSTR &pszOut)
    {
    CHAR szTemp[MAX_URL_LEN] = {'\0'};
    int iOutLen = 0; for (int i = 0; i < iLen; i ++)
    {
    if (pcszIn[i] >= 'A' && pcszIn[i] <= 'Z') // A-Z
    {
    szTemp[iOutLen] = pcszIn[i];
    iOutLen += 1;
    }
    else if (pcszIn[i] >= 'a' && pcszIn[i] <= 'z') // a-z
    {
    szTemp[iOutLen] = pcszIn[i];
    iOutLen += 1;
    }
    else if (pcszIn[i] >= '0' && pcszIn[i] <= '9') // 0-9
    {
    szTemp[iOutLen] = pcszIn[i];
    iOutLen += 1;
    }
    else if (
       pcszIn[i] == '@' 
    || pcszIn[i] == '*' 
    || pcszIn[i] == '_' 
    || pcszIn[i] == '-' 
    || pcszIn[i] == '.')
    {
    szTemp[iOutLen] = pcszIn[i];
    iOutLen += 1;
    }
    else if (pcszIn[i] == ' ')
    {
    szTemp[iOutLen] = '+';
    iOutLen += 1;
    }
    else
    {
    sprintf(szTemp + iOutLen, "%%%02X", pcszIn[i] & 0xFF);
    iOutLen += 3;
    }
    } szTemp[iOutLen] = '\0'; pszOut = new CHAR[iOutLen + 1];
    memcpy(pszOut, szTemp, iOutLen + 1); return iOutLen;
    }用法:
    CString strURL = _T("http://www.google.com/search?hl=zh-CN&q=");
    CString strKeywords = _T("游戏 下载");
    LPSTR pszKeywordsUTF8 = NULL;
    LPSTR pszKeywordsEncoded = NULL;
    int iLen = 0;// Convert to UTF-8 keywords
    iLen = ConvertCodePage(CP_ACP, strKeywords, strKeywords.GetLength(), CP_UTF8, pszKeywordsUTF8);if (NULL == pszKeywordsUTF8)
    {
    return;
    }// Encode keywords
    iLen = EncodeURL(pszKeywordsUTF8, iLen, pszKeywordsEncoded);delete[] pszKeywordsUTF8;if (NULL == pszKeywordsEncoded)
    {
    return;
    }strURL += pszKeywordsEncoded;delete[] pszKeywordsEncoded;::ShellExecute(NULL, _T("open"), strURL, NULL, NULL, SW_SHOWNORMAL);