当前位置:网站首页>double型数据转字符串后通过MCU串口发送
double型数据转字符串后通过MCU串口发送
2022-08-03 05:24:00 【langaopeng】
double型数据转字符串如下函数,函数的功能如:将 double=12.123的数据转为CString = "12.123";后通过串口发出
void PutDataTOChar(double dat)
{
u8 i;
u8 tab[10];//存整数部分
u8 bot[6];//存小数部分
u8 iIndex =0;//整数部分指针
u8 jIndex =0;//小数部分指针
int aa = 0;
double pot = 0.0;
for(i=0;i<10;i++)
{
tab[i] = ' ';
bot[i] = ' ';
}
aa = (int) dat;//取整数
if(aa < 0) //是负数//
{
dat = -dat;
aa = -aa;
pot = dat-aa;
while(aa/10 > 0)//将int 型转为字符型
{
tab[iIndex++] = aa%10; // 取出个位
aa /= 10;
}
tab[iIndex] = aa;
for(i=0;i<5;i++)//将小数部分转为字符型
{
bot[jIndex++] = (int)(pot*10);
pot = pot*10 - (int)(pot*10);
}
putchar('-');
for(i=iIndex;i>0;i--)
{
putchar(tab[i]+0x30);
}
putchar(tab[i]+0x30);
putchar('.');
for(i=0;i<jIndex;i++)
{
putchar(bot[i]+0x30);
}
}
else
{
while(aa/10 > 0)
{
tab[iIndex++] = aa%10; // 取出个位
aa /= 10;
}
tab[iIndex] = aa;
for(i=0;i<5;i++)
{
bot[jIndex++] = (int)(pot*10);
pot = pot*10 - (int)(pot*10);
}
for(i=iIndex;i>0;i--)
{
putchar(tab[i]+0x30);
}
putchar(tab[i]+0x30);
putchar('.');
for(i=0;i<jIndex;i++)
{
putchar(bot[i]+0x30);
}
}
}
边栏推荐
猜你喜欢
网络间通信
Execute the mysql script file in the docker mysql container and solve the garbled characters
自监督论文阅读笔记 DetCo: Unsupervised Contrastive Learning for Object Detection
Gradle插件与代理服务器导致Sync Project失败的问题
滚动条 scrollbar 和scrollbar-thumb 样式
Kettle Spoon 安装配置详解
MySql 怎么查出符合条件的最新的数据行?
嵌入式实验三(代码几乎都要改才能运行)
微信小程序 自定义tabBar
【源码解读】你买的NFT到底是什么?
随机推荐
动漫:海贼王女
Qlik Sense 聚合函数及范围详解(Sum、Count、All、ToTaL、{1})
自监督论文阅读笔记SELF-SUPERVISED SPECTRAL MATCHING NETWORK FOR HYPERSPECTRAL TARGET DETECTION
自监督论文阅读笔记Reading and Writing: Discriminative and Generative Modelingfor Self-Supervised Text Recogn
自监督论文阅读笔记 Incremental-DETR:Incremental Few-Shot Object Detection via Self-Supervised Learning
该描述怎么写成SQL语句
enum和enum class的区别
中国认证认可服务行业“十四五”发展规划及经营模式分析报告2022~2028年
详解背包问题(DP分支)
Oracle 注释详解(--、/**/、rem)
HoloLens联合发明人:打造理想的全天AR需要解决这些问题
Playing with Markdown(2) - Extraction and Manipulation of Abstract Syntax Trees
【 Nmap and Metasploit common commands 】
Kettle Spoon 安装配置详解
c#,.net 下载文件 设置断点
对象の使用
自监督论文阅读笔记FIAD net: a Fast SAR ship detection network based on feature integration attention and self
中国水环境治理行业投融资分析及“十四五”规划建议报告2022~2028年
A.1#【内存管理】——1.1.1 node:struct pglist_data
【第三周】ResNet+ResNeXt