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];

}

 

原文地址:http://www.cnblogs.com/xnyzie/p/16876359.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性