utf8编码到unicode编码

wy6728582 2007-12-27 05:00:14
请求各位大哥给出utf8到unicode的算法,在下先表示谢了
...全文
143 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wy6728582 2007-12-28
  • 打赏
  • 举报
回复
多谢各位的帮助,在下向各位道谢了!
cad_vc 2007-12-27
  • 打赏
  • 举报
回复
int wNum = MultiByteToWideChar(CP_UTF8, 0, pStr, -1, NULL, 0);
WCHAR pWstr = new WCHAR[wNum +1];
MultiByteToWideChar(CP_UTF8, 0, pStr, -1, pWstr , wNum);
思危 2007-12-27
  • 打赏
  • 举报
回复
如果不用API,vcbase上有段转换算法,直接给你转贴过来了,或许有用

WCHAR* CXmlProcess::UTF_8ToUnicode(char *ustart) //把UTF-8转换成Unicode
{
char char_one;
char char_two;
char char_three;
int Hchar;
int Lchar;
char uchar[2];
WCHAR *unicode;
CString string_one;
CString string_two;
CString string_three;
CString combiString;
char_one = *ustart;
char_two = *(ustart+1);
char_three = *(ustart+2);
string_one.Format("%x",char_one);
string_two.Format("%x",char_two);
string_three.Format("%x",char_three);
string_three = string_three.Right(2);
string_two = string_two.Right(2);
string_one = string_one.Right(2);
string_three = HexToBin(string_three.Left(1))+HexToBin(string_three.Right(1));
string_two = HexToBin(string_two.Left(1))+HexToBin(string_two.Right(1));
string_one = HexToBin(string_one.Left(1))+HexToBin(string_one.Right(1));
combiString = string_one +string_two +string_three;
combiString = combiString.Right(20);
combiString.Delete(4,2);
combiString.Delete(10,2);
Hchar = BinToInt(combiString.Left(8));
Lchar = BinToInt(combiString.Right(8));
uchar[1] = (char)Hchar;
uchar[0] = (char)Lchar;
unicode = (WCHAR *)uchar;
return unicode;
}
思危 2007-12-27
  • 打赏
  • 举报
回复
UTF8 是 Unicode 的变种,MultiByteToWideChar不能转吧。如果是在Window上用可以直接用1楼兄弟给出的API,
但要include 头文件 Winldap.h, 并且在项目属性里添加 Wldap32.lib的链接,或是直接用#pragma comment( lib, "Winldap" )声明


ToperRay 2007-12-27
  • 打赏
  • 举报
回复
MultiByteToWideChar就行了

UTF8是MultiByte
jwybobo2007 2007-12-27
  • 打赏
  • 举报
回复
另附说明

Parameters
lpSrcStr
[in] Pointer to a null-terminated UTF-8 string to convert.
cchSrc
[in] Specifies the number of characters in the lpSrcStr string.
lpDestStr
[out] Pointer to a buffer that receives the converted Unicode string.
cchDest
[in] Specifies the size, in characters, of the lpDestStr buffer.
Return Values
The return value is the number of characters written to the lpDestStr buffer.

If the lpDestStr buffer is too small, GetLastError returns ERROR_INSUFFICIENT_BUFFER.

Requirements
Windows NT/2000/XP: Included in Windows NT 4.0 SP4 and later.
Redistributable: Requires Active Directory Client Extension on Windows 95/98.
Header: Declared in Winldap.h.
Library: Use Wldap32.lib
jwybobo2007 2007-12-27
  • 打赏
  • 举报
回复
int LdapUTF8ToUnicode(
LPCSTR lpSrcStr,
int cchSrc,
LPWSTR lpDestStr,
int cchDest
);

哥们用这个API函数吧,直接转

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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