[英]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.