首页 > 其他分享 >将hex printf输出存储到变量

将hex printf输出存储到变量

时间:2022-11-10 10:31:50浏览次数:48  
标签:十六进制 存储 temp int decimal hex sprintf printf decimalNumber

I have to round off a float to decimal. After rounding off, I should convert this number to hexadecimal. I think I got the round off part okay with round()

我必须将浮点数舍入为十进制。四舍五入后,我应该将此数字转换为十六进制。我认为圆形部分可以得到圆形部分()

Is there a way to convert a decimal to hexadecimal in C, and store it into a part of an array? I'm thinking of the concept on how printf() converts the decimal to hex.

有没有办法在C中将十进制转换为十六进制,并将其存储到数组的一部分?我正在考虑printf()如何将十进制转换为十六进制的概念。

What I have in mind is something like this:

我的想法是这样的:

float k = 10.123;

int a;

unsigned char var_store[1];

unsigned char array_t[3];

array_t[0] = 0x01;

array_t[1] = 0x04;

a = round(k);

var_store[0] = sprintf("%x",a);

array_t[2] = var_store[0];

but I'm having a

但我有一个

warning passing argument 2 of 'sprintf' makes pointer from integer without a cast

警告传递'sprintf'的参数2使得整数指针没有强制转换

I'm not sure if this is the way to do it. But I think this is relatively straight forward. Thanks

我不确定这是不是这样做的。但我认为这是相对直接的。谢谢

2 个解决方案

#1


2  

People tend to get very confused with the term "hexadecimal". It should mean "the number as a human-readable ascii string with digits 0-F", but because raw binary data is typically presented in hex, people miuse it to mean the binary data itself. Whilst of course you can write a function that converts a decimal number, expressed as a string, to a hexadecimal number, expressed as another string, it's fiddly and, except as a learning exercise, pointless thing to do. sprintf converts C variables to human-readable strings for you. To get a decimal, pass "%d", to get hex, pass "%x". You also need to pass a destination buffer, like this.

人们倾向于对术语“十六进制”感到困惑。它应该表示“数字为人类可读的ascii字符串,数字为0-F”,但由于原始二进制数据通常以十六进制表示,人们将其称为二进制数据本身。当然,您可以编写一个函数,将表示为字符串的十进制数转换为十六进制数,表示为另一个字符串,它是繁琐的,除了作为学习练习外,无意义的事情要做。 sprintf为您将C变量转换为人类可读的字符串。要获得小数,请传递“%d”,以获取十六进制,传递“%x”。您还需要传递目标缓冲区,如下所示。

char destination[256];

int a = 123;

sprintf(destination, "number is decimal %d hex %x", a, a);

#2


0  

I did not recollect any library function.

我没有回忆任何库函数。

But the traditional mathematical way is below. I you want you can create a user defined function.

但传统的数学方法如下。我希望你能创建一个用户定义的函数。

#include <iostream>


using namespace std;


int main()

{

    long int decimalNumber = 2567888;

    char hexadecimalNumber[100];

    int temp;

    int i =1;


    while(decimalNumber!=0)

    {

         temp = decimalNumber % 16;


      //To convert integer into character

      if( temp < 10)

           temp =temp + 48;

      else

         temp = temp + 55;


      hexadecimalNumber[i++]= temp;

      decimalNumber = decimalNumber / 16;

    }


    for(int j = i -1 ;j> 0;j--)

      cout<<hexadecimalNumber[j];

}

#1


2  

People tend to get very confused with the term "hexadecimal". It should mean "the number as a human-readable ascii string with digits 0-F", but because raw binary data is typically presented in hex, people miuse it to mean the binary data itself. Whilst of course you can write a function that converts a decimal number, expressed as a string, to a hexadecimal number, expressed as another string, it's fiddly and, except as a learning exercise, pointless thing to do. sprintf converts C variables to human-readable strings for you. To get a decimal, pass "%d", to get hex, pass "%x". You also need to pass a destination buffer, like this.

人们倾向于对术语“十六进制”感到困惑。它应该表示“数字为人类可读的ascii字符串,数字为0-F”,但由于原始二进制数据通常以十六进制表示,人们将其称为二进制数据本身。当然,您可以编写一个函数,将表示为字符串的十进制数转换为十六进制数,表示为另一个字符串,它是繁琐的,除了作为学习练习外,无意义的事情要做。 sprintf为您将C变量转换为人类可读的字符串。要获得小数,请传递“%d”,以获取十六进制,传递“%x”。您还需要传递目标缓冲区,如下所示。

char destination[256];

int a = 123;

sprintf(destination, "number is decimal %d hex %x", a, a);

#2


0  

I did not recollect any library function.

我没有回忆任何库函数。

But the traditional mathematical way is below. I you want you can create a user defined function.

但传统的数学方法如下。我希望你能创建一个用户定义的函数。

#include <iostream>


using namespace std;


int main()

{

    long int decimalNumber = 2567888;

    char hexadecimalNumber[100];

    int temp;

    int i =1;


    while(decimalNumber!=0)

    {

         temp = decimalNumber % 16;


      //To convert integer into character

      if( temp < 10)

           temp =temp + 48;

      else

         temp = temp + 55;


      hexadecimalNumber[i++]= temp;

      decimalNumber = decimalNumber / 16;

    }


    for(int j = i -1 ;j> 0;j--)

      cout<<hexadecimalNumber[j];

}


标签:十六进制,存储,temp,int,decimal,hex,sprintf,printf,decimalNumber
From: https://blog.51cto.com/u_15848821/5839976

相关文章