转换字符用十六进制值设置为String [英] Convert char with hex value to String

查看:165
本文介绍了转换字符用十六进制值设置为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有类似下面的字符:

If I have a character like the following:

wchar_t c = '\x0A'

什么是一个简单的方法,使之成为像将它转换:

What's an easy way to convert it so that it becomes something like:

wchar_t dst[] ==> "0A"

基本上,C的十六进制值变成一个字符串值。

Basically, the hex value of c becomes a string value.

推荐答案

的ç的积分值将的0x0A 10 在基地10)。您可以使用的sprintf 格式化为十六进制:

The integral value of c would be 0x0A (10 in base 10). You can use sprintf to format it as hex:

wchar_t c = '\x0A';
int c_val = c;
char string[3];

sprintf( string, "%.2X", c_val );

注意中间 c_val 变量是不需要的,不仅增加了透明度

Note the intermediate c_val variable is not needed, only added for clarity

也可以手动做到这一点:

or you can do it manually:

int c_low = c & 0x0F;
int c_high = ( c & 0xF0 ) >> 4;

...translate c_low and c_high to its textual representation...

这篇关于转换字符用十六进制值设置为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆