经过近几天尝试了多种方法,可以通过以下三种方式实现16进制转成中文输出,utf8编码占3个字节,gb2312编码占2个字节,具体方法如下:
方法一:
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include<algorithm>
#include<cstdlib>
using namespace std;
unsigned char ch2hex(char ch)
{
static const char* hex = "0123456789abcdef";
for (unsigned char i = 0;i != 16;++i)
if (ch == hex[i])
return i;
return 0;
}
char* solve(char* dest, const char* src)
{
int i = 0;
int cnt = 0;
unsigned char* d = (unsigned char*)dest;
while (*src)
{
if (i & 1)
{
d[cnt++] |= ch2hex(*src);
&n

本文介绍了三种C语言实现16进制转为中文输出的方法,包括直接转换、使用locale函数和通过stringstream操作。示例代码经过VS2019测试,能成功处理utf8和gb2312编码的中文字符。注意,utf8编码在不支持的终端中可能显示为乱码。

926

被折叠的 条评论
为什么被折叠?



