GB2312或UTF-8如何转ISO-8859-1 本帖最后由 oyljerry 于 2013-10-14 10:15:12 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ::MultiByteToWideChar(936,0,...::WideCharToMultiByte(28591,NULL,... 不需要宏转换936就gbk28591就是ISO-8859-1 的代码页用对上面两个API函数就ok了 不好意思啊,我的编码是GB2312转ISO-8859-1,GB2312对应的整型值是多少啊 写了个函数如下,但是转出来似乎不正确,大神帮忙看一下吧,辛苦了void Gb2312ToISO8859_1(char* pstrOut, int dwOutLen, const char* pstrIn, int dwInLen) { memset(pstrOut, '\0', dwOutLen);#ifdef WIN32 int i = MultiByteToWideChar(936, 0, pstrIn, -1, NULL, 0); wchar_t * strSrc = new wchar_t[i+1]; MultiByteToWideChar(936, 0, pstrIn, -1, strSrc, i); i = WideCharToMultiByte(28591, 0, strSrc, -1, NULL, 0, NULL, NULL); if (i >= dwOutLen) { i = dwOutLen - 1; } WideCharToMultiByte(28591, 0, strSrc, -1, pstrOut, i, NULL, NULL); delete strSrc; } 看看我这个,VC6,一直用这个,疗效不错。void ToolsClass::ConvertGBKToUtf8(CString &strGBK){ int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0); unsigned short * wszUtf8 = new unsigned short[len+1]; memset(wszUtf8, 0, len * 2 + 2); MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len); len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL); char *szUtf8=new char[len + 1]; memset(szUtf8, 0, len + 1); WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL); strGBK = szUtf8; delete[] szUtf8; delete[] wszUtf8;}void ToolsClass::ConvertUtf8ToGBK(CString &strUtf8){ int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0); unsigned short * wszGBK = new unsigned short[len+1]; memset(wszGBK, 0, len * 2 + 2); MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL); char *szGBK=new char[len + 1]; memset(szGBK, 0, len + 1); WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL); strUtf8 = szGBK; delete[] szGBK; delete[] wszGBK;} ISO-8859-1 对应的代码页是1252http://msdn.microsoft.com/en-us/library/ms537500%28v=vs.85%29.aspx 我用下面函数转了(已经改成1252了),但是转出来的还是??,有源码不?呵呵,谢了先void Gb2312ToISO8859_1(char* pstrOut, int dwOutLen, const char* pstrIn, int dwInLen) { memset(pstrOut, '\0', dwOutLen);#ifdef WIN32 int i = MultiByteToWideChar(936, 0, pstrIn, -1, NULL, 0); wchar_t * strSrc = new wchar_t[i+1]; MultiByteToWideChar(936, 0, pstrIn, -1, strSrc, i); i = WideCharToMultiByte(28591, 0, strSrc, -1, NULL, 0, NULL, NULL); if (i >= dwOutLen) { i = dwOutLen - 1; } WideCharToMultiByte(28591, 0, strSrc, -1, pstrOut, i, NULL, NULL); delete strSrc; } 用法是对的但是无法直接转需要先转换成UNICODE UTF-16,然后再转换,一次好像无法转换的吧 iconv更强大点libiconv字符集转换库使用方法 http://www.usidcbbs.com/read.php?tid=1504 如果通过编程修改组策略中的安全选项? CreateProcess移植问题 如何在对话框上建立以一个子窗口(这个子窗口是需要注册的形式) 一个奇怪问题 BT下载极品飞车7,有人要吗?留Email 怎样实现象Window优化大师的界面? 请教:VC++生成的几种DLL的具体区别? 我该学习什么? ++++数据源的注册问题+++++ COM/DCOM可以调用其他的dll文件吗? 请教如何按下列方式重写OnInitDialog()函数 请问opencv对图像的色彩直方图显示//rate=0.151216是什么意思?
::WideCharToMultiByte(28591,NULL,...
936就gbk
28591就是ISO-8859-1 的代码页
用对上面两个API函数就ok了
void Gb2312ToISO8859_1(char* pstrOut, int dwOutLen, const char* pstrIn, int dwInLen)
{
memset(pstrOut, '\0', dwOutLen);
#ifdef WIN32
int i = MultiByteToWideChar(936, 0, pstrIn, -1, NULL, 0);
wchar_t * strSrc = new wchar_t[i+1];
MultiByteToWideChar(936, 0, pstrIn, -1, strSrc, i); i = WideCharToMultiByte(28591, 0, strSrc, -1, NULL, 0, NULL, NULL);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
WideCharToMultiByte(28591, 0, strSrc, -1, pstrOut, i, NULL, NULL);
delete strSrc;
}
void ToolsClass::ConvertGBKToUtf8(CString &strGBK)
{
int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0);
unsigned short * wszUtf8 = new unsigned short[len+1];
memset(wszUtf8, 0, len * 2 + 2);
MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len); len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
char *szUtf8=new char[len + 1];
memset(szUtf8, 0, len + 1);
WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL); strGBK = szUtf8;
delete[] szUtf8;
delete[] wszUtf8;}void ToolsClass::ConvertUtf8ToGBK(CString &strUtf8)
{
int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0);
unsigned short * wszGBK = new unsigned short[len+1];
memset(wszGBK, 0, len * 2 + 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len); len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
char *szGBK=new char[len + 1];
memset(szGBK, 0, len + 1);
WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL); strUtf8 = szGBK;
delete[] szGBK;
delete[] wszGBK;
}
void Gb2312ToISO8859_1(char* pstrOut, int dwOutLen, const char* pstrIn, int dwInLen)
{
memset(pstrOut, '\0', dwOutLen);
#ifdef WIN32
int i = MultiByteToWideChar(936, 0, pstrIn, -1, NULL, 0);
wchar_t * strSrc = new wchar_t[i+1];
MultiByteToWideChar(936, 0, pstrIn, -1, strSrc, i); i = WideCharToMultiByte(28591, 0, strSrc, -1, NULL, 0, NULL, NULL);
if (i >= dwOutLen)
{
i = dwOutLen - 1;
}
WideCharToMultiByte(28591, 0, strSrc, -1, pstrOut, i, NULL, NULL);
delete strSrc;
}
但是无法直接转
需要先转换成UNICODE UTF-16,然后再转换,一次好像无法转换的吧