将hex printf输出存储到变量

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


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


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


目录
相关文章
|
2天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
4天前
|
云安全 数据采集 人工智能
古茗联名引爆全网,阿里云三层防护助力对抗黑产
阿里云三层校验+风险识别,为古茗每一杯奶茶保驾护航!
古茗联名引爆全网,阿里云三层防护助力对抗黑产
|
4天前
|
存储 机器学习/深度学习 人工智能
大模型微调技术:LoRA原理与实践
本文深入解析大语言模型微调中的关键技术——低秩自适应(LoRA)。通过分析全参数微调的计算瓶颈,详细阐述LoRA的数学原理、实现机制和优势特点。文章包含完整的PyTorch实现代码、性能对比实验以及实际应用场景,为开发者提供高效微调大模型的实践指南。
537 2
kde
|
4天前
|
人工智能 关系型数据库 PostgreSQL
n8n Docker 部署手册
n8n是一款开源工作流自动化平台,支持低代码与可编程模式,集成400+服务节点,原生支持AI与API连接,可自托管部署,助力团队构建安全高效的自动化流程。
kde
362 3
|
2天前
|
Linux 虚拟化 iOS开发
VMware Workstation Pro 25H2 for Windows & Linux - 领先的免费桌面虚拟化软件
VMware Workstation Pro 25H2 for Windows & Linux - 领先的免费桌面虚拟化软件
754 4
VMware Workstation Pro 25H2 for Windows & Linux - 领先的免费桌面虚拟化软件
|
3天前
|
JavaScript 开发工具 Android开发
如何在原生 App 中调用 Uniapp 的页面?
如何在原生 App 中调用 Uniapp 的页面?
243 138
|
4天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段四:学术分析 AI 项目 RAG 落地指南:基于 Spring AI 的本地与阿里云知识库实践
本文介绍RAG(检索增强生成)技术,结合Spring AI与本地及云知识库实现学术分析AI应用,利用阿里云Qwen-Plus模型提升回答准确性与可信度。
254 91
AI 超级智能体全栈项目阶段四:学术分析 AI 项目 RAG 落地指南:基于 Spring AI 的本地与阿里云知识库实践