C编程:将Hex Int转换为Char * [英] C Programming: Convert Hex Int to Char*

查看:228
本文介绍了C编程:将Hex Int转换为Char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我将如何转换类似:

  int i = 0x11111111; 

到字符指针?我尝试使用itoa()函数,但它给了我一个浮点异常。

解决方案

是非标准的。远离。



一种可能性是使用 sprintf 和hexa的正确格式说明符 x and do:

  char str [BIG_ENOUGH + 1]; 
sprintf(str,%x,value);但是,这个计算的问题是值的大小 数组。您必须使用一些猜测,常见问题12.21 是一个很好的起点。



表示任何基本 b 中的数字所需的字符数可以通过以下公式近似:


⌈ log b (n + 1)⌉


如果需要,再添加一对以保存 0x ,然后您的 BIG_ENOUGH 已准备就绪。


My question is how I would go about converting something like:

    int i = 0x11111111;

to a character pointer? I tried using the itoa() function but it gave me a floating-point exception.

解决方案

itoa is non-standard. Stay away.

One possibility is to use sprintf and the proper format specifier for hexa i.e. x and do:

char str[ BIG_ENOUGH + 1 ];
sprintf(str,"%x",value);

However, the problem with this computing the size of the value array. You have to do with some guesses and FAQ 12.21 is a good starting point.

The number of characters required to represent a number in any base b can be approximated by the following formula:

⌈logb(n + 1)⌉

Add a couple more to hold the 0x, if need be, and then your BIG_ENOUGH is ready.

这篇关于C编程:将Hex Int转换为Char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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