utf8串输出中文

bool Utf8ToZhong(const std::string &utf8_str, std::string *result_str) {
	if(utf8_str.size() == 0) {
		return false;
	}
	int move_flag = 0;
	string str;
	int start = 0,end = utf8_str.size();
	char str_zhong[128];
	int str_it = 0;
	while(start < end) {
		if(utf8_str[start] == '%') {
			if(move_flag == 0) {//汉字起始,计算一个汉字是几个字节
				int y = 0; 
				y = HexChar2Dec(utf8_str[start+1]);
				y = (y<<4) +HexChar2Dec(utf8_str[start+2]);
				int flag = 7; 
				while(flag > 0) { 
					if((y&(1<<flag)) == 0) { 
						break;
					}    
					flag--;
				}    
				move_flag = 7-flag;//总共几个字节
			}    
			start++;
		}    
		else {
			if(move_flag == 0) { 
				str += utf8_str[start];
				start++;
			}    
			else {
				int y = 0; 
				y = HexChar2Dec(utf8_str[start]);
				y = (y<<4) +HexChar2Dec(utf8_str[start+1]);
				str_zhong[str_it] = y; 
				str += str_zhong[str_it];
				str_it++;
				start += 2;//偏移掉一个%
				if(move_flag > 0) {
					move_flag--;  //处理一个字节,消掉一个字节
				}
			}
		}
	}
	*result_str = str;
	return true;
}

`OutputDebugStringA`函数是用于向调试器输出I字符串的,而不是UTF-8字符串。如果你想要输出UTF-8的字符串,你需要将UTF-8字符串换为ANSI字符串,然后再调用`OutputDebugStringA`函数。 以下是一个示例代码,演示如何将UTF-8字符串转换为ANSI字符串输出到调试器: ```cpp #include <windows.h> #include <iostream> #include <string> void OutputDebugStringUTF8(const std::string& utf8String) { int length = MultiByteToWideChar(CP_UTF8, 0, utf8String.c_str(), -1, NULL, 0); if (length == 0) { std::cout << "Failed to convert UTF-8 string to wide string." << std::endl; return; } std::wstring wideString(length, L'\0'); MultiByteToWideChar(CP_UTF8, 0, utf8String.c_str(), -1, &wideString[0], length); int ansiLength = WideCharToMultiByte(CP_ACP, 0, wideString.c_str(), -1, NULL, 0, NULL, NULL); if (ansiLength == 0) { std::cout << "Failed to convert wide string to ANSI string." << std::endl; return; } std::string ansiString(ansiLength, '\0'); WideCharToMultiByte(CP_ACP, 0, wideString.c_str(), -1, &ansiString[0], ansiLength, NULL, NULL); OutputDebugStringA(ansiString.c_str()); } int main() { std::string utf8String = u8"你好,世界!"; OutputDebugStringUTF8(utf8String); return 0; } ``` 在这个示例代码中,我们定义了一个名为`OutputDebugStringUTF8`的函数,用于输出UTF-8字符串到调试器。 首先,我们使用`MultiByteToWideChar`函数将UTF-8字符串转换为宽字符串(UTF-16)。我们首先调用一次`MultiByteToWideChar`函数来获取转换后的宽字符串的长度,然后创建一个足够容纳宽字符串的`std::wstring`对象,并再次调用`MultiByteToWideChar`函数进行实际的转换。 然后,我们使用`WideCharToMultiByte`函数将宽字符串转换为ANSI字符串。我们首先调用一次`WideCharToMultiByte`函数来获取转换后的ANSI字符串的长度,然后创建一个足够容纳ANSI字符串的`std::string`对象,并再次调用`WideCharToMultiByte`函数进行实际的转换。 最后,我们调用`OutputDebugStringA`函数来输出ANSI字符串到调试器。 在`main`函数中,我们定义了一个UTF-8字符串`utf8String`,然后调用`OutputDebugStringUTF8`函数将其输出到调试器。 希望这个示例代码可以帮助你将UTF-8字符串输出到调试器。如果有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值