我的头文件和库文件都是从编译TCLSH84里提取出来的,但嵌入到C++中后可选择的编码类型就剩下utf-8 identity unicode这三个了,但TCLSH84.exe里却有很多.因为编码不同,处理中文的时候就得转换,查了下手册,里面说用Tcl_ExternalToUtf()可以完成这个功能,但试了好几次还是乱码 =.=|||  不知道是不是我用法错了(哪位有sample的话让我参考下吧).

最后在网上看到用encoding convertfrom identity $str来转换编码.结果确实很好,代码如下:
 1#include "Program.h"
 2#include <tcl/tcl.h>
 3#include <string>
 4
 5using namespace std;
 6
 7int main()
 8{
 9    string text = "df今天的天气传说是不错的好像。df";
10    string code = "set text [encoding convertfrom identity $text] \n set ret [string range $text 1 5]";
11
12    Tcl_Interp *interp = Tcl_CreateInterp();
13
14    // get all encoding name
15      Tcl_GetEncodingNames(interp);  
16    cout << Tcl_GetStringResult(interp) << endl;
17
18    // setup parameters
19    assert(Tcl_SetVar(interp, "text", text.c_str(), TCL_GLOBAL_ONLY));
20    assert(Tcl_SetVar(interp, "ret""", TCL_GLOBAL_ONLY));
21
22    // eval script
23    Tcl_Eval(interp, code.c_str());
24
25    // get result
26    char *ret;
27    ret = (char *)Tcl_GetVar(interp, "ret", TCL_GLOBAL_ONLY);
28
29    cout << "ret:" << ret << endl;
30
31    system("pause");
32
33    Tcl_DeleteInterp(interp);
34}

    但当我在MFC中用的时候却又出现乱码了...太不解了...大家给帮帮忙吧:
 1UINT CHTMLVampireDlg::HttpRequestFunc(LPVOID pParam)
 2{
 3    CHTMLVampireDlg *dlg = (CHTMLVampireDlg*)pParam;
 4    CString url;
 5    dlg->comboBox_urlList.GetWindowText(url);
 6
 7    try
 8    {
 9        CInternetSession session;
10        CHttpConnection* con = new CHttpConnection(&session, url);
11        CString htmlCode, tmpStr;
12        CHttpFile* file = con->OpenRequest(CHttpConnection::HTTP_VERB_GET, "");
13        file->SendRequest();
14        while(file->ReadString(tmpStr))
15        {
16            htmlCode += tmpStr;
17        }

18
19        ///* 当网页编码为utf-8时启用如下代码 */
20        ///* 转换为正确的编码类型 */

21        //char* buf = htmlCode.GetBuffer();
22        //htmlCode.ReleaseBuffer();
23        //wchar_t* wbuffer = 0;
24        //int wbufferLen = MultiByteToWideChar(CP_UTF8, 0, buf, -1, 0, 0);
25        //wbuffer = new wchar_t[wbufferLen];
26        //memset(wbuffer, 0, sizeof(wchar_t)*wbufferLen);
27        //MultiByteToWideChar(CP_UTF8, 0, buf, -1, wbuffer, wbufferLen);
28
29        ///* 将宽字符转换为ASCII */
30        //char *ascii = 0;
31        //int asciiLen = WideCharToMultiByte(CP_ACP, 0, wbuffer, -1, 0, 0, 0, 0);
32        //ascii = new char[asciiLen];
33        //memset(ascii, 0, sizeof(char)*asciiLen);
34        //WideCharToMultiByte(CP_ACP, 0, wbuffer, -1, ascii, asciiLen, 0, 0);
35        
36        dlg->richEdit_htmlCode.SetWindowText(htmlCode);
37        //delete[] ascii;
38
39        //delete[] wbuffer;
40
41        file->Close();
42        delete file;
43        con->Close();
44        delete con;
45        session.Close();
46
47    }

48    catch(CInternetException* excep)
49    {
50        CString errMsg;
51        excep->GetErrorMessage((LPTSTR)(LPCTSTR)errMsg, 0);
52        dlg->SetWindowText(url + "  " + errMsg);
53        excep->ReportError();
54        excep->Delete();
55    }

56
57    return 0;
58}

59
60
61void CHTMLVampireDlg::OnBnClickedButtonExecute()
62{
63    // TODO: 在此添加控件通知处理程序代码
64    CString tclcode, htmlcode;
65    this->edit_htmlCode.GetWindowText(tclcode);
66    this->richEdit_htmlCode.GetWindowText(htmlcode);
67
68    
69
70    Tcl_Interp* interp = Tcl_CreateInterp();
71
72    ASSERT(Tcl_SetVar(interp, "htmlCode", (LPCSTR)htmlcode, TCL_GLOBAL_ONLY));
73    ASSERT(Tcl_SetVar(interp, "ret""", TCL_GLOBAL_ONLY));
74    if(Tcl_Eval(interp, (LPCTSTR)tclcode) == TCL_OK)
75    {
76        char* ret = (char*)Tcl_GetVar(interp, "ret", TCL_GLOBAL_ONLY);
77        ASSERT(ret);
78        this->richEdit_htmlCode.SetWindowText(ret);
79    }

80    else
81    {
82        AfxMessageBox(Tcl_GetStringResult(interp), 00);
83    }

84
85    Tcl_DeleteInterp(interp);
86}
 posted on 2007-08-28 00:29  *Alacky  阅读(3012)  评论(0编辑  收藏  举报