将hex printf输出存储到变量

简介: 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()

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

}

相关文章
|
5月前
|
程序员 编译器 C语言
用printf函数输出数据
用printf函数输出数据
35 2
|
5月前
|
C语言
使用printf函数输出数据
在C语言中,printf函数是一个常用的标准库函数,用于在控制台输出格式化的字符串和数据。它允许我们按照指定的格式输出各种类型的数据,包括整数、浮点数、字符和字符串等。
43 0
|
5月前
从接口获取获取到数组arr=[‘1‘,‘a‘,‘2‘,‘b‘,‘3‘,‘c‘]转换成{number:‘123’,char:‘abc’}
从接口获取获取到数组arr=[‘1‘,‘a‘,‘2‘,‘b‘,‘3‘,‘c‘]转换成{number:‘123’,char:‘abc’}
|
11月前
|
C语言
C语言printf格式化打印(%d、%md、%f、%c、%s、%o、%x、%p、%e、%E等)
C语言printf格式化打印(%d、%md、%f、%c、%s、%o、%x、%p、%e、%E等)
|
12月前
|
存储
将hex printf输出存储到变量
将hex printf输出存储到变量
|
存储
关于char类型数组的两种输出方法
关于char类型数组的两种输出方法
253 0
|
XML C语言 数据格式
C基础——使用printf打印各种数据类型的方式(示例)
C基础——使用printf打印各种数据类型的方式(示例)
729 0
C基础——使用printf打印各种数据类型的方式(示例)
|
C语言 Python
C语言:定义一个计算两个整数的和的函数int sum(int a,int b),在主函数中输入两个整数x和y,调用sum(x,y)输出x+y的和。
C语言:定义一个计算两个整数的和的函数int sum(int a,int b),在主函数中输入两个整数x和y,调用sum(x,y)输出x+y的和。
1208 0
C语言:定义一个计算两个整数的和的函数int sum(int a,int b),在主函数中输入两个整数x和y,调用sum(x,y)输出x+y的和。
|
人工智能 BI
两个字符串 char* a, char* b,输出b在a中的位置次序。
/** 题目: 两个字符串 char* a, char* b,输出b在a中的位置次序。 void output_postion(const char* a, const char* b); 如:a = "abdbcc" b = "abc" ...
1052 0