当前位置:网站首页>MCU接收串口字符型数据转换成数据型数据
MCU接收串口字符型数据转换成数据型数据
2022-08-03 05:24:00 【langaopeng】
做MCU开发时,常常碰到串口接收的数据是字符型数据,是ASCII码,而我们需要解析的是数据型的。如:串口接收到的是:"-236,854,635"这个字符串。而我们需要将这个字符串解析为:int 的-236,854,635才能为我所用。
以下是实现功能的子函数。
void PutCharTOdata(double *tab,u8 *rev_tab,u8 rev_count)// tab[ ] 为转换后的数据,rev_tab[ ]为转换前的一个个字符,rev_count为字符串长度(字符的个数)
{
u8 i;
u8 pflag = 0;
u8 point = 0;
u8 F_flag =0;
for(i=0;i<10;i++) tab[i] =0.0;
for(i=0;i<rev_count;i++)
{
if((F_flag==0)&&(rev_tab[i] == '-'))//判断是否为负号
{
F_flag = 1;
}
else if(rev_tab[i]!=',')//判断是否碰到逗号
{
if(rev_tab[i]!='.')//判断是否碰到小数点
{
if(pflag)
{
tab[point] = (double)(rev_tab[i]-0x30)/(pow(10,pflag))+tab[point];
pflag ++;
}
else
{
tab[point] = (rev_tab[i]-0x30)+tab[point]*10;
}
}
else
{
pflag ++;
}
}
else if(rev_tab[i]==',')
{
if(F_flag ==1) tab[point] = -tab[point];
point++;
pflag =0;
F_flag = 0;
}
}
if(F_flag ==1) tab[point] = -tab[point];
}
此函数的功能为将rev_tab[ ] = "-236,854,635";这个字符串解析为了:
tab[0] =-236;
tab[1] = 854;
tab[2] = 635;
边栏推荐
猜你喜欢
自监督论文阅读笔记Efficient Self-supervised Vision Pretraining with Local Masked Reconstruction
自监督论文阅读笔记DisCo: Remedy Self-supervised Learning on Lightweight Models with Distilled Contrastive
Kettle Spoon 安装配置详解
当奈飞的NFT忘记了web2的业务安全
Android学习 | 08.SQLiteOpenHelper
损失函数(第五周)
IPC 通信 - IPC
卷积神经网络入门
边缘辅助无人机网络的分层联邦学习
交叉熵(第六周)
随机推荐
opencv
动漫 吞噬星空
softmax和最大熵
【第一周】深度学习和pytorch基础
【第三周】ResNet+ResNeXt
建立平衡二叉树简单demo
The result of request.getParameter is on
该描述怎么写成SQL语句
中国聚氯乙烯(PVC)土工膜发展动态及投资前景预测报告2022~2028年
B.1#【编程语言】—1 arm 汇编指令
当我们在看Etherscan的时候,到底在看什么?
极光推送 能否缓存 消息
The ` monorepo ` ` hoist ` mechanism lead to the change of the loading configuration file path
设备树(devicetree)-dts语法
自监督论文阅读笔记 Self-Supervised Deep Learning for Vehicle Detection in High-Resolution Satellite Imagery
Execute the mysql script file in the docker mysql container and solve the garbled characters
Try setting CHROME_EXECUTABLE to a Chrome executable
时间盲注脚本
用户登录验证程序的实现
Android学习 | 08.SQLiteOpenHelper