繁体   English   中英

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

[英]Trouble with converting dec into hex for C

仅使用以下库:

#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);

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

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

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

没有返回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

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

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

至于其余的,数字是一个数字; “转换”为十六进制只对输出有意义。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM