如何将 UTF-8 编码转换成 ANSI 编码?

msl 2002-11-18 09:38:02
我转换的时候基本上正确,但是有些空格会转换成"?",有些却不会,不知道为什么?
...全文
2011 12 打赏 收藏 转发到动态 举报
写回复
用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

3,055

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧