简体   繁体   English

不使用“printf”将 Int 转换为十六进制(C 编程)

[英]Convert Int to Hexdecimal without using 'printf' (C Programming)

Is it possible to Convert Int to Hexdecimal without using 'printf'?是否可以在不使用“printf”的情况下将 Int 转换为十六进制?

Best if the all the value are placed in the variable itself and some sample code with explanation.最好将所有值都放在变量本身和一些带有解释的示例代码中。

The decimal and hexadecimal systems are just ways of expressing the value of the int. 十进制和十六进制只是表示int值的方式。 In a way "it is already a hexadecimal". 以某种方式“它已经是十六进制”。

I think you can use itoa in stdlib.h : 我认为您可以在stdlib.h中使用itoa:

char * itoa ( int value, char * str, int base ); or sprintf(str,"%x",value);

The documentation : itoa documentation 文档: itoa文档

Of course it is possible. 当然可以。 Just think about how printf itself was originally implemented... 只要考虑一下printf本身是如何实现的...

I won't give you a full solution, only hints, based on which you can experiment with the implementation in code: 我不会为您提供完整的解决方案,仅提供提示,您可以根据这些提示尝试代码中的实现:

An 8-bit number can be expressed as 2 hex digits, which contain the higher and lower 4 bits, respectively. 一个8位数字可以表示为2个十六进制数字,分别包含高4位和低4位。 To get these bits, you can use the bit-shift ( >> ) and mask ( & ) operators. 要获取这些位,可以使用移位( >> )和掩码( & )运算符。

Once you have a 4-bit value, you can easily map it to the correct hex digit using a sequence of if s, or (as a more elegant solution) by indexing into a character array. 一旦有了4位值,就可以使用if s序列或(作为更优雅的解决方案)通过索引到字符数组来轻松将其映射到正确的十六进制数字。

Hexdecival vs Decimal vs Binary..etc.. are only different bases that represent the same number. 十六进制vs十进制vs二进制等。只是代表相同数字的不同基数。 printf doesn't convert your number, it generates an hexdecimal string representation of your number. printf不会转换您的数字,它会生成您的数字的十六进制字符串表示形式。 If you want to implement your own study how to make a conversion between decimal and hexdecimal bases. 如果要实施自己的研究,如何在十进制和十六进制之间进行转换。

Yes , it is definitely possible to convert an integer to a hexadecimal string without using the "printf" family of formatting functions. 是的 ,绝对可以将整数转换为十六进制字符串,而无需使用格式函数的“ printf”系列。

You can write such a function by converting the number from base-10 (as we think about it) to base-16 (hexadecimal) and printing the digits in that representation ( 0-9A-F ). 您可以编写这样的函数,方法是将数字从以10为基数(我们认为是)转换为以16为基数(十六进制),然后以该表示形式打印数字( 0-9A-F )。 This will require you to think a lot about the bases we use to represent numbers . 这将需要您考虑很多我们用来表示数字的基础

If you are referring to displaying an int as a hexadecimal number in C, than you will have to write a function that does the same thing as printf. 如果要使用C将十六进制数字显示为int,则必须编写一个与printf相同的函数。

If you are referring to casting or internal representation, it can't be done because hexadecimal is not a data type. 如果要引用转换或内部表示形式,则无法完成,因为十六进制不是数据类型。

An int is stored in your computer as a binary number. 一个int作为二进制数存储在您的计算机中。 In fact, since hex can be interpreted as a shorthand for writing binary, you might even say that when you print out a decimal number using printf, it has to be converted from hex to decimal. 实际上,由于十六进制可以解释为编写二进制的简写,您甚至可以说,当使用printf打印出十进制数时,必须将其十六进制转换十进制。

it's an example for convert a char array to hex string format这是一个将 char 数组转换为十六进制字符串格式的示例

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

unsigned char d=255;
char hex_array[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char *array_to_hex_string(uint8_t data[],int size);
char test_data[3] = {'0',188,255};
int main()
{

    printf("%s",array_to_hex_string(test_data,3));
    return 0;
 }

 char *array_to_hex_string(uint8_t data[],int size){
 int i,j;
 char *hex_string = (char *)malloc((2*size) * sizeof(data[0]));
for(i=0;i<size;i++){
    hex_string[j] = hex_array[(data[i]>>4)];
    hex_string[j+1] =  hex_array[(data[i] & 15)];

    j +=2;
        }
return (char *)hex_string;
}
cout << hex << intvar << endl;

但是,如果您想要一个答案可以给您的作业A分,那您就不会很幸运了:)

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

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