string与utf8编码问题

threadroc 2014-04-21 03:02:51
我C++调用一个curl插件访问网页获取utf8格式的数据流,
c#接收代码如下:

private Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, Object extraData)
{
rc += System.Text.Encoding.GetEncoding(this.encoding).GetString(buf);
return size * nmemb;
}

C++接收代码如下:

size_t WriteCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
string *buff = (string*) data;
buff->append((char *) ptr, size * nmemb);
return size* nmemb;
}

C++目前接收输出后是乱码,不识别汉字,怎么才能以UTF8格式或GB18030格式接收呀?
谢谢指教.
...全文
364 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-04-21
  • 打赏
  • 举报
回复
仅供参考,尽管是VB6:
Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
'常用的代码页:
const cpUTF8   =65001
const cpGB2312 =  936
const cpGB18030=54936
const cpUTF7   =65000
Function MultiByteToUTF16(UTF8() As Byte, CodePage As Long) As String
    Dim bufSize As Long
    bufSize = MultiByteToWideChar(CodePage, 0&, UTF8(0), UBound(UTF8) + 1, 0, 0)
    MultiByteToUTF16 = Space(bufSize)
    MultiByteToWideChar CodePage, 0&, UTF8(0), UBound(UTF8) + 1, StrPtr(MultiByteToUTF16), bufSize
End Function

Function UTF16ToMultiByte(UTF16 As String, CodePage As Long) As Byte()
    Dim bufSize As Long
    Dim arr() As Byte
    bufSize = WideCharToMultiByte(CodePage, 0&, StrPtr(UTF16), Len(UTF16), 0, 0, 0, 0)
    ReDim arr(bufSize - 1)
    WideCharToMultiByte CodePage, 0&, StrPtr(UTF16), Len(UTF16), arr(0), bufSize, 0, 0
    UTF16ToMultiByte = arr
End Function

Private Sub Command1_Click()
    MsgBox MultiByteToUTF16(UTF16ToMultiByte("ab中,c", cpUTF8), cpUTF8)
End Sub

赵4老师 2014-04-21
  • 打赏
  • 举报
回复
MultiByteToWideChar The MultiByteToWideChar function maps a character string to a wide-character (Unicode) string. The character string mapped by this function is not necessarily from a multibyte character set. int MultiByteToWideChar( UINT CodePage, // code page DWORD dwFlags, // character-type options LPCSTR lpMultiByteStr, // address of string to map int cchMultiByte, // number of bytes in string LPWSTR lpWideCharStr, // address of wide-character buffer int cchWideChar // size of buffer ); Parameters CodePage Specifies the code page to be used to perform the conversion. This parameter can be given the value of any code page that is installed or available in the system. You can also specify one of the following values: Value Meaning CP_ACP ANSI code page CP_MACCP Macintosh code page CP_OEMCP OEM code page CP_SYMBOL Symbol code page (42) CP_THREAD_ACP The current thread's ANSI code page CP_UTF7 Translate using UTF-7 CP_UTF8 Translate using UTF-8 dwFlags A set of bit flags that indicate whether to translate to precomposed or composite wide characters (if a composite form exists), whether to use glyph characters in place of control characters, and how to deal with invalid characters. You can specify a combination of the following flag constants: Value Meaning MB_PRECOMPOSED Always use precomposed characters — that is, characters in which a base character and a nonspacing character have a single character value. This is the default translation option. Cannot be used with MB_COMPOSITE. MB_COMPOSITE Always use composite characters — that is, characters in which a base character and a nonspacing character have different character values. Cannot be used with MB_PRECOMPOSED. MB_ERR_INVALID_CHARS If the function encounters an invalid input character, it fails and GetLastError returns ERROR_NO_UNICODE_TRANSLATION. MB_USEGLYPHCHARS Use glyph characters instead of control characters. A composite character consists of a base character and a nonspacing character, each having different character values. A precomposed character has a single character value for a base/non-spacing character combination. In the character è, the e is the base character and the accent grave mark is the nonspacing character. The function's default behavior is to translate to the precomposed form. If a precomposed form does not exist, the function attempts to translate to a composite form. The flags MB_PRECOMPOSED and MB_COMPOSITE are mutually exclusive. The MB_USEGLYPHCHARS flag and the MB_ERR_INVALID_CHARS can be set regardless of the state of the other flags. lpMultiByteStr Points to the character string to be converted. cchMultiByte Specifies the size in bytes of the string pointed to by the lpMultiByteStr parameter. If this value is –1, the string is assumed to be null terminated and the length is calculated automatically. lpWideCharStr Points to a buffer that receives the translated string. cchWideChar Specifies the size, in wide characters, of the buffer pointed to by the lpWideCharStr parameter. If this value is zero, the function returns the required buffer size, in wide characters, and makes no use of the lpWideCharStr buffer. Return Values If the function succeeds, and cchWideChar is nonzero, the return value is the number of wide characters written to the buffer pointed to by lpWideCharStr. If the function succeeds, and cchWideChar is zero, the return value is the required size, in wide characters, for a buffer that can receive the translated string. If the function fails, the return value is zero. To get extended error information, call GetLastError. GetLastError may return one of the following error codes: ERROR_INSUFFICIENT_BUFFER ERROR_INVALID_FLAGS ERROR_INVALID_PARAMETER ERROR_NO_UNICODE_TRANSLATION Remarks The lpMultiByteStr and lpWideCharStr pointers must not be the same. If they are the same, the function fails, and GetLastError returns the value ERROR_INVALID_PARAMETER. The function fails if MB_ERR_INVALID_CHARS is set and it encounters an invalid character in the source string. An invalid character is one that would translate to the default character if MB_ERR_INVALID_CHARS was not set, but is not the default character in the source string, or when a lead byte is found in a string and there is no valid trail byte for DBCS strings. When an invalid character is found, and MB_ERR_INVALID_CHARS is set, the function returns 0 and sets GetLastError with the error ERROR_NO_UNICODE_TRANSLATION. Windows CE: Windows CE does not support the CP_UTF7 and CP_UTF8 values for the CodePage parameter. QuickInfo Windows NT: Requires version 3.1 or later. Windows: Requires Windows 95 or later. Windows CE: Requires version 1.0 or later. Header: Declared in winnls.h. Import Library: Use kernel32.lib. See Also Unicode and Character Sets Overview, Unicode and Character Set Functions, WideCharToMultiByte
threadroc 2014-04-21
  • 打赏
  • 举报
回复
引用 4 楼 zhao4zhong1 的回复:
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 GBK:0xB0 0xA1,Unicode-16 LE:0x4A 0x55,Unicode-16 BE:0x55 0x4A,UTF-8:0xE5 0x95 0x8A
谢谢这位大大,关键是,我怎么利用你告诉我的这些知识去把人脑看不懂的乱码改成人脑能看懂的汉字呢?
xyj19871987 2014-04-21
  • 打赏
  • 举报
回复
帮顶 顺便mark一下
赵4老师 2014-04-21
  • 打赏
  • 举报
回复
对电脑而言没有乱码,只有二进制字节;对人脑才有乱码。啊 GBK:0xB0 0xA1,Unicode-16 LE:0x4A 0x55,Unicode-16 BE:0x55 0x4A,UTF-8:0xE5 0x95 0x8A
threadroc 2014-04-21
  • 打赏
  • 举报
回复
乱码如下,只有中文是乱码. <!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" content="text/html;charset=utf-8"><link rel="dns-prefetch" href="//s1.bdstatic.com"/><link rel="dns-prefetch" href="//t1.baidu.com"/><link rel="dns-prefetch" href="//t2.baidu.com"/><link rel="dns-prefetch" href="//t3.baidu.com"/><link rel="dns-prefetch" href="//t10.baidu.com"/><link rel="dns-prefetch" href="//t11.baidu.com"/><link rel="dns-prefetch" href="//t12.baidu.com"/><title>鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾</title><style >html,body{height:100%}html{overflow-y:auto}#wrapper{position:relative;_position:;min-
threadroc 2014-04-21
  • 打赏
  • 举报
回复
引用 1 楼 qq120848369 的回复:
乱码是因为你终端配置的编码和文本编码不同造成的,调整你的终端编码为utf8就可以看见utf8编码的文本了
那如果我一个我
引用 1 楼 qq120848369 的回复:
乱码是因为你终端配置的编码和文本编码不同造成的,调整你的终端编码为utf8就可以看见utf8编码的文本了
我的不是终端,是MFC,你指的是setlocal吗,那个也不管用的
qq120848369 2014-04-21
  • 打赏
  • 举报
回复
乱码是因为你终端配置的编码和文本编码不同造成的,调整你的终端编码为utf8就可以看见utf8编码的文本了

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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