In the project, it is necessary to use C++ to convert Chinese characters to hexadecimal. For example,
if you hit "Chinese" on UE, you will get "D6D0B9FAC8CB0D0A" in hexadecimal. How to convert this, and how to convert char str[]= "D6D0B9FAC8CB0D0A"; Change it to Chinese characters and put it in the array of char[10]={0}?
#include<stdio.h>
#include <string.h>
#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);
}
else
{
d[cnt]=ch2hex(*src)<<4;
}
src++;
i++;
}
return dest;
}
string tohex(const string& str)
{
string ret;
static const char *hex="0123456789ABCDEF";
for(int i=0;i!=str.size();++i)
{
ret.push_back(hex[(str[i]>>4)&0xf]);
ret.push_back( hex[str[i]&0xf]);
}
return ret;
}
int main()
{
cout<<tohex("中国人")<<endl;
char dest[24]={0},src[]="D6D0B9FAC8CB0D0A";
puts(solve(dest,src));
system("pause");
return 0;
}
Reference:
https://bbs.csdn.net/topics/390027033