将dec转换为十六进制为C的麻烦

时间:2011-04-17 06:34:15

标签: c hex

仅使用以下库:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <math.h>

我需要一种有效的方法将小数转换为十六进制:

int iteration (arguments...) {
//calculate the number of iterations for this function to do something
return iterations; //returns in decimal, e.g. 70
}

char hello = (iteration in hex);

代码(十六进制迭代)的代码是什么样的?

我有很多循环所以会有很多转换为十六进制,效率越高越好(虽然我很确定有这样的功能)。

3 个答案:

答案 0 :(得分:1)

如果我理解你的问题,我相信你想要的是什么     printf(“%x”,迭代); 要么     printf(“%X”,迭代);

答案 1 :(得分:1)

没有回复int作为小数的事情。 int在C中有一个内部表示,与我们表示人类数字的方式没什么关系。

在C中,您可以将以八进制,十进制或十六进制表示法编号的数字分配给int。然后它包含“抽象”数字可以这么说。

int a = 073; // starting with 0, interpreted as octal
int b = 23;  // starting with another digit, decimal
int c = 0xA3 // starting with 0x, hexadecimal

答案 2 :(得分:0)

整数的最后一个字节可赋值给char

char hello = iteration(...) & 0xff;

至于其余部分,数字是一个数字; “转换”到十六进制只对输出很重要。

相关问题