使用JavaScript中 encodeURIComponent() 函数对中文进行的编码在VC6中如何解码,有对应的函数吗在网上找了一段代码
#include <afx.h>
#include <iostream>
void UTF8ToGB(CString& str);void ANSIToGB(char* str,int n)
{
ASSERT(str!=NULL); // 保证传进来的参数不能为NULL
wchar_t szwchar = 0;
CString szResult,szhead = "", szend = "";
CString szrst;
char ch, hex[2] = "";
int ix = 0;
szResult = str;
int imax = szResult.GetLength();
int ih = szResult.Find("%", 0);
int ie = szResult.ReverseFind('%');
szhead = szResult.Left(ih);
//szend = szResault.Right(imax - ie - 3);
szResult = "";
ix = ih;
CString strTemp;
bool bIsHaveUTF8 = false;
while (ch = *(str + ix))
{
if (ch == '%')
{
hex[0] = *(str + ix + 1);
hex[1] = *(str + ix + 2);
sscanf(hex, "%x", &szwchar);
szrst += szwchar;
ix+=3;
bIsHaveUTF8 = true;
}
else
{
if(bIsHaveUTF8)
{
UTF8ToGB(szrst);
strTemp+=szrst;
szrst="";
bIsHaveUTF8 = false;
}
// 取出不必转换的字符
strTemp += *(str + ix);
ix++;
}
}
szResult = szhead + strTemp;
memset(str,0,n);
strcpy(str,szResult);}void UTF8ToGB(CString& szstr)
{
WCHAR* strSrc; TCHAR* szRes;
int i = MultiByteToWideChar(CP_UTF8, 0, szstr, -1, NULL, 0); strSrc = new WCHAR[i + 1];
MultiByteToWideChar(CP_UTF8, 0, szstr, -1, strSrc, i); i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
szRes = new TCHAR[i + 1];
WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);
szstr = szRes;
delete[]strSrc;
delete[]szRes;
}int main(int argc, char* argv[])
{
//str = "%E6%96%B0%E5%BB%BA";
char str[] = "MFC%E8%8B%B1%E6%96%87%E6%89%8B%E5%86%8C.chm";
// 注意,这里传给ANSIToGB的第一个参数一定不能是个常量字符串,// 因为ANSIToGB内部还要从第一个参数将结果返回// 当然这些只是细节,不值得斤斤计较,大家可以修改成自己合适的,比如,解码后的结果可以通过其他参数传出....ANSIToGB(str,strlen(str)*sizeof(char)); printf("结果是:%s\n", str);return 0;
}把这段代码放到我自己的程序中(我自己的程序开发环境是VC6.0)在debug模式下编译能通过,结果也是正确的,但是在release模式下编译就会出错(红色显示的两行)错误是MultiByteToWideChar函数的第三个参数也就是szstr 'MultiByteToWideChar' : cannot convert parameter 3 from 'Class String ' to 'const char *',这是为什么?
#include <afx.h>
#include <iostream>
void UTF8ToGB(CString& str);void ANSIToGB(char* str,int n)
{
ASSERT(str!=NULL); // 保证传进来的参数不能为NULL
wchar_t szwchar = 0;
CString szResult,szhead = "", szend = "";
CString szrst;
char ch, hex[2] = "";
int ix = 0;
szResult = str;
int imax = szResult.GetLength();
int ih = szResult.Find("%", 0);
int ie = szResult.ReverseFind('%');
szhead = szResult.Left(ih);
//szend = szResault.Right(imax - ie - 3);
szResult = "";
ix = ih;
CString strTemp;
bool bIsHaveUTF8 = false;
while (ch = *(str + ix))
{
if (ch == '%')
{
hex[0] = *(str + ix + 1);
hex[1] = *(str + ix + 2);
sscanf(hex, "%x", &szwchar);
szrst += szwchar;
ix+=3;
bIsHaveUTF8 = true;
}
else
{
if(bIsHaveUTF8)
{
UTF8ToGB(szrst);
strTemp+=szrst;
szrst="";
bIsHaveUTF8 = false;
}
// 取出不必转换的字符
strTemp += *(str + ix);
ix++;
}
}
szResult = szhead + strTemp;
memset(str,0,n);
strcpy(str,szResult);}void UTF8ToGB(CString& szstr)
{
WCHAR* strSrc; TCHAR* szRes;
int i = MultiByteToWideChar(CP_UTF8, 0, szstr, -1, NULL, 0); strSrc = new WCHAR[i + 1];
MultiByteToWideChar(CP_UTF8, 0, szstr, -1, strSrc, i); i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
szRes = new TCHAR[i + 1];
WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);
szstr = szRes;
delete[]strSrc;
delete[]szRes;
}int main(int argc, char* argv[])
{
//str = "%E6%96%B0%E5%BB%BA";
char str[] = "MFC%E8%8B%B1%E6%96%87%E6%89%8B%E5%86%8C.chm";
// 注意,这里传给ANSIToGB的第一个参数一定不能是个常量字符串,// 因为ANSIToGB内部还要从第一个参数将结果返回// 当然这些只是细节,不值得斤斤计较,大家可以修改成自己合适的,比如,解码后的结果可以通过其他参数传出....ANSIToGB(str,strlen(str)*sizeof(char)); printf("结果是:%s\n", str);return 0;
}把这段代码放到我自己的程序中(我自己的程序开发环境是VC6.0)在debug模式下编译能通过,结果也是正确的,但是在release模式下编译就会出错(红色显示的两行)错误是MultiByteToWideChar函数的第三个参数也就是szstr 'MultiByteToWideChar' : cannot convert parameter 3 from 'Class String ' to 'const char *',这是为什么?
int result = 0;
try
{
//int needlen = WideCharToMultiByte( CP_ACP, 0, unicodestr, -1, NULL, 0, NULL, NULL );
int needlen = WideCharToMultiByte( g____ansi_codepage, 0, unicodestr, -1, NULL, 0, NULL, NULL );
if( needlen < 0 )
{
return needlen;
} //result = WideCharToMultiByte( CP_ACP, 0, unicodestr, -1, ansistr, needlen + 1, NULL, NULL );
result = WideCharToMultiByte( g____ansi_codepage, 0, unicodestr, -1, ansistr, needlen + 1, NULL, NULL );
if( result < 0 )
{
return result;
}
return strlen( ansistr );
}
catch( ... )
{
ShowError();
}
return result;
jennyvenus
是MultiByteToWideChar函数,不是WideCharToMultiByte