社区
HTML/XML
帖子详情
如何将 UTF-8 编码转换成 ANSI 编码?
msl
2002-11-18 09:38:02
我转换的时候基本上正确,但是有些空格会转换成"?",有些却不会,不知道为什么?
...全文
2011
12
打赏
收藏
如何将 UTF-8 编码转换成 ANSI 编码?
我转换的时候基本上正确,但是有些空格会转换成"?",有些却不会,不知道为什么?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
12 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
Sunny_wh
2003-01-13
打赏
举报
回复
public String getStr(String str)
{
try
{
String temp_p = str;
byte[] temp_t = temp_p.getBytes("ANSI");
String temp = new String(temp_t);
return temp;
}catch(Exception e)
{
}
return "null";
}
ruxming
2002-12-27
打赏
举报
回复
Convert utf8 to htmel
http://www.codeguru.com/clipboard/HowToPasteHtml.html
ruxming
2002-12-27
打赏
举报
回复
About article was contributed by George Ter-Saakov.
--------
ruxming
2002-12-27
打赏
举报
回复
1.What is UTF-8?
http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
2.Here is a UTF8ToHtml function, which converts from UTF-8 to HTML.
//utf8 - pointer to UTF8 formatted text. dwSize - size of UTF8 text; ptr is the pointer to Output buffer.
//The OnClickedPastehtml is the handler for BN_CLICK event of the button in Dialog box. IDC_TEXT is the multiline text box.
void UTF8ToHtml(BYTE *utf8, DWORD dwSize, CHAR *ptr )
{
int code;
BYTE *end = utf8 + dwSize;
while( utf8 < end )
{
code = 0;
if( (*utf8 & 0xF0) == 0xF0 )
{
code = (((*utf8)&0x0F) << 18) | (((*(utf8+1))
& 0x7F)<<12) | (((*(utf8+2)) & 0x7F)<<6)
| ((*(utf8+3)) & 0x7F );
utf8+=3;
}
else
{
if( (*utf8 & 0xE0) == 0xE0 )
{
code = (((*utf8)&0x1F) << 12) | (((*(utf8+1))
& 0x7F)<<6 ) | ((*(utf8+2)) & 0x7F );
utf8+=2;
}
else
{
if( (*utf8 & 0xC0) == 0xC0 )
{
code = (((*utf8)&0x3F) << 6) | ((*(utf8+1)) & 0x7F) ;
utf8+=1;
}
}
}
if( code == 0 )
{
*ptr = *utf8;
}
else
{
char s[10];
switch(code)
{
case 160:
strcpy(s, "& ");
break;
case 34:
strcpy(s, "&");
break;
case 36:
strcpy( s, "&&");
break;
case 60:
strcpy( s, "&<");
break;
case 62:
strcpy( s, "&>");
break;
default:
sprintf( s, "&#%d;", code );
break;
}
strcpy( ptr, s );
ptr += strlen(s)-1;
}
utf8++;
ptr++;
}
*ptr = 0;
}
LRESULT CDialog::OnClickedPastehtml( WORD wNotifyCode,
WORD wID,
HWND hWndCtl,
BOOL& bHandled)
{
if (!OpenClipboard() )
return 0;
UINT uHtmlFormat = RegisterClipboardFormat("HTML Format");
UINT uFormat = uHtmlFormat;
if( IsClipboardFormatAvailable( uHtmlFormat ) == FALSE )
{
if( IsClipboardFormatAvailable( CF_TEXT ) == FALSE )
return 0;
uFormat = CF_TEXT;
}
HGLOBAL hglb;
LPTSTR lptstr;
hglb = GetClipboardData(uFormat);
if (hglb != NULL)
{
lptstr = (LPTSTR)GlobalLock(hglb);
if (lptstr != NULL)
{
char *ptr1 = strstr( lptstr, "<!--StartFragment-->");
if( ptr1 != 0 )
{
ptr1 += 20;
char * ptr2 = strstr( lptstr, "<!--EndFragment-->");
int iSize = (ptr2 - ptr1);
char * tmp = (char*)_alloca( iSize *2);
UTF8ToHtml((BYTE*)ptr1, iSize, tmp );
//memcpy(tmp, ptr1, iSize );
//tmp[iSize] = 0;
SetDlgItemText(IDC_TEXT, tmp );
}
else
SetDlgItemText(IDC_TEXT, lptstr );
GlobalUnlock(hglb);
}
}
CloseClipboard();
return 0;
}
microran2000
2002-12-27
打赏
举报
回复
先使用MultiByteToWideChar 用CF-UTF8 参数转化成 UNICODE,然后再把unicode转化成ansi(此时可以用W2A()).
microran2000
2002-12-26
打赏
举报
回复
我认Widechar和UTF8是两个概念,对于UTF8,一个汉字占三个字节,一个希腊字母占用两个字节,一个英文字母或数字占用1个字节。而WideChar即Unicode则所有字符占用两个字节。包括英文字母。使用WideCharToMultiByte参数是要求使用CP_UTF8参数实现转化。建议在使用前先使用NOTEPAD建立一个文本文件,分别输入几个汉字、希腊字母、英文字母、数字,然后使用不同的格式存盘,ANSI、 UNICODE、 UTF8 然后使用Visual C++以二进制方式打开文本文件,查看十六进制编码。UNICODE 不等于UTF8,上面的W2A以及wcstombs只适用于UNICODE,WideCharToMultiByte和W2A也不等价,WideCharToMultiByte提供了更多的参数,实现各种编码的转化。W2A只是ATL定义的一个宏充其量是WideCharToMultiByte众多参数版本中的一个。
下面我把发送ICQ短信的部分代码粘出来,自己分析一下ANSI UNICODE UTF8之间的转化
strcpy(szSubMessage,pThis->m_szMessage.Mid(nCurrent,(nMessageCount-nCurrent)>=nCharCountMax?nCharCountMax:remainder));
nCurrent+=nCharCountMax;
szRefer="Referer: ";
szRefer+=szHost;
szRefer+="\r\n";
szHost="https://web.icq.com/secure/sms/send_history/1,,,00.html";
lstrcpyW(wszChinese,A2W(szSubMessage));
WideCharToMultiByte(CP_UTF8,0,wszChinese,lstrlenW(wszChinese)
,szTemp,sizeof szTemp,NULL,NULL);
EscapeToCString(szTarget,szTemp);
if(pThis->m_bFlash)
szPostData="uSend=1&prefix=+86&tophone=%MOBILE%&msg=\1%E5%86%89%E6%9E%97%E4%BB%93\1";
else
szPostData="uSend=1&prefix=+86&tophone=%MOBILE%&msg=%E5%86%89%E6%9E%97%E4%BB%93";
szPostData.Replace ("%MOBILE%",pThis->m_szMobile.Right (11));
szPostData.Replace ("86",pThis->m_szCountry);
szPostData.Replace ("%E5%86%89%E6%9E%97%E4%BB%93",szTarget);
szHeaders="Accept: *//*\r\n"
"User-Agent: Mozilla/4.0\r\n"
"Content-Type: application/x-www-form-urlencoded\r\n"
"Content-Length: %LENGTH%\r\n";
arvid_gs
2002-12-25
打赏
举报
回复
WideCharToMultiByte or W2A()
lyy_hill
2002-12-23
打赏
举报
回复
用WideCharToMultiByte吧,功能强大。想怎么转都行。
realdreamer
2002-12-20
打赏
举报
回复
size_t wcstombs(
char *mbstr,
const wchar_t *wcstr,
size_t count
);
Requirements
Routine Required header Compatibility
wcstombs <stdlib.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
For additional compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Example
/* WCSTOMBS.C illustrates the behavior of the wcstombs function. */
#include <stdio.h>
#include <stdlib.h>
void main( void )
{
int i;
char *pmbbuf = (char *)malloc( MB_CUR_MAX );
wchar_t *pwchello = L"Hello, world.";
printf( "Convert wide-character string:\n" );
i = wcstombs( pmbbuf, pwchello, MB_CUR_MAX );
printf( "\tCharacters converted: %u\n", i );
printf( "\tMultibyte character: %s\n\n", pmbbuf );
}
phoenixsoft
2002-12-20
打赏
举报
回复
see too
xuefl66
2002-12-09
打赏
举报
回复
see
xuefl66
2002-11-29
打赏
举报
回复
see
see
文本文件
编码
转换:
ANSI
、Unicode、
UTF-8
相互转换(修改版)
ANSI
--> Unicode
ANSI
--> Unicode big endian
ANSI
-->
UTF-8
Unicode -->
ANSI
Unicode -->
UTF-8
UTF-8
-->
ANSI
UTF-8
--> Unicode
UTF-8
--> Unicode big endian
ansi
转别的,不检验BOM,一律作为
ansi
编码
进行转换 unicode转别的,首先检验BOM,不合格不转换 utf8转别的,首先检验BOM,不合格不转换
UTF-8
转
ANSI
文本文件转换器
可批量将
UTF-8
的文本文件转为
ANSI
文本文件,代码略加修改,即可在换任意两种
编码
的文本文件内互相转化。
lua utf8 unicode
ansi
转换
require "lc" print(lc.help()); Simple Characters Transformation a2w(
ansi
to unicode) u2w(utf8 to unicode) w2a(unicode to
ansi
) w2u(unicode to utf8) u2a(utf8 to
ansi
) a2u(
ansi
to utf8) bstr(bytes of str) help(show this) wunoman@qq.com 2012/03/06
完美转换
UTF-8
到 GB2312(源代码和详细说明)
很多人用source insight 打开某些源码文件时,汉字显示为一堆乱码。这个问题是因为
编码
方式不同。记事本和一些编辑器默认
编码
方式是
ANSI
,在这种方式下输入汉字,其实就是GB系列的
编码
方式。不幸的是,广收欢迎的代码查看工具Source insight 虽然支持汉字,但是它不支持
UTF-8
。笔者感到疑惑的是,当初开发source insight的这帮人现在哪里去了?为何不继续开发?这么好的工具,却不再更新了,实在让人可惜。 可惜归可惜,程序还是要看。乱码怎么办?很多人用笨方法,用记事本打开源代码,再另存为
ANSI
格式的文件。如果文件只有几个,倒是既简单又实惠,然而,当你碰到成百上千的代码文件都是
UTF-8
时,你就会觉得这是梦魇了。 不过,不要怕。现在,完美的解决方法来了。
ANSI
编码
和
UTF-8
的区别
ANSI
:英文1个字节,汉字2个字节。共16384个字符。GBK
编码
UTF-8
:是Unicode一种压缩形式,英文1个字节,汉字3个字节。 显然中文UTF8不如
ANSI
合算,这就是中国的网页用作
ANSI
编码
而老外的网页常用
UTF-8
的原因。 建议使用
UTF-8
编码
。 用记事本转换
UTF-8
会在文件开头添加0xBFBBEF。 注意:如果在项目中读取txt文件,txt文件内容输入或者无法识别。可以将UFT-8格式转换为
ANSI
格式以便识别文件中内容。 ...
HTML/XML
3,055
社区成员
8,066
社区内容
发帖
与我相关
我的任务
HTML/XML
VC/MFC HTML/XML
复制链接
扫一扫
分享
社区描述
VC/MFC HTML/XML
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章