当前位置:网站首页>c语言实现float型数据转成BCD数据
c语言实现float型数据转成BCD数据
2022-07-28 10:32:00 【tutu-hu】
一.简述
使用BCD编码方式可以更大限度的节省字节空间,使用四位二进制保存一个十进制数。本文完成了float型数据转换为BCD编码方式的转换,并且根据保留小数点后的位数做出对应的转换。
二.代码实现
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/** * @brief 将float转为bcd * * @param input:输入的float型数据 * decimal_num:要保留的小数位数 * output_buf:输出缓存区 * output_num:最终转换输出字节数 * * @return void * */
void float_to_bcd(const float input, int decimal_num, char* output_buf,int *output_num)
{
int i = 0;
int len_temp = 0;
int len_final = 0; //最中组成的strbuf长度
char string_buffer[20] = {
0}; //定义临时缓存区
//根据保留的小数位数做不同操作
switch (decimal_num)
{
case 0:
sprintf_s(string_buffer, "%.0f", input); //将输入的float型变量进行格式化输出
break;
case 1:
sprintf_s(string_buffer, "%.2f", input); //将输入的float型变量进行格式化输出
len_temp = strlen(string_buffer); //获取格式化后的数据长度
memmove(string_buffer + len_temp - 3, string_buffer + len_temp - 2, 1); //内存移动,可以重叠
string_buffer[len_temp - 2] = 0; //添加结束符
break;
case 2://两位小数部分
sprintf_s(string_buffer, "%.3f", input); //将输入的float型变量进行格式化输出
len_temp = strlen(string_buffer); //获取格式化后的数据长度
memmove(string_buffer + len_temp - 4, string_buffer + len_temp - 3, 2); //内存移动,可以重叠
string_buffer[len_temp - 2] = 0; //添加结束符
break;
case 3:
sprintf_s(string_buffer, "%.4f", input); //将输入的float型变量进行格式化输出
len_temp = strlen(string_buffer); //获取格式化后的数据长度
memmove(string_buffer + len_temp - 5, string_buffer + len_temp - 4, 3); //内存移动,可以重叠
string_buffer[len_temp - 2] = 0; //添加结束符
break;
default:break;
}
len_final = strlen(string_buffer);
printf("len_final = %d string_buffer = %s\n", len_final, string_buffer);
//string到BCD的转换
if (len_final % 2) //一共有奇数位
{
output_buf[0] = string_buffer[0] - '0';
for (i = 0; i < (len_final - 1) / 2; i++)
{
output_buf[i + 1] = (string_buffer[2 * i + 1] - '0') << 4 | (string_buffer[2 * i + 2] - '0');
}
}
else //一共有偶数位
{
for (i = 0; i < len_final / 2; i++)
{
output_buf[i] = (string_buffer[2 * i] - '0') << 4 | (string_buffer[2 * i + 1] - '0');
}
}
*output_num = len_final % 2 + len_final / 2; //获取最终产生的字节数
}
int main(void)
{
int i = 0;
int len_final = 0;
float a = 12142.01023;
char bcd_buffer[10];
//float_to_bcd(a, 0, bcd_buffer, &len_final); //保留0位小数
//float_to_bcd(a, 1, bcd_buffer, &len_final); //保留1位小数
//float_to_bcd(a, 2, bcd_buffer, &len_final); //保留2位小数
float_to_bcd(a, 3, bcd_buffer, &len_final); //保留3位小数
for (i = 0; i < len_final; i++)
{
printf("%x ", bcd_buffer[i]);
}
return 0;
}三.执行结果
//保留0位小数
len_final = 5 string_buffer = 12142
输出的BCD数据:01 21 42
//保留1位小数
len_final = 6 string_buffer = 121420
输出的BCD数据:12 14 20
//保留2位小数
len_final = 7 string_buffer = 1214201
输出的BCD数据:01 21 42 01
//保留3位小数
len_final = 8 string_buffer = 12142009
输出的BCD数据:12 14 20 09边栏推荐
- JSON初步理解
- Advance.ai sailing guide helps enterprises sail to Indonesia and grasp half of the Southeast Asian market
- 产品端数据分析思维
- CTF skill tree - file upload
- 3. MapReduce explanation and source code analysis
- 两年CRUD,二本毕业,备战两个月面试阿里,侥幸拿下offer定级P6
- andorid 开发
- 从零开始Blazor Server(2)--整合数据库
- 一文学会如何做电商数据分析(附运营分析指标框架)
- Learn how to do e-commerce data analysis (with operation analysis index framework)
猜你喜欢

Installation points and precautions of split angle probe

Batch Normlization

Advance.ai sailing guide helps enterprises sail to Indonesia and grasp half of the Southeast Asian market

Attention 注意力机制流程框图

分体式测斜探头安装要点及注意事项

一文学会如何做电商数据分析(附运营分析指标框架)

The 10th Landbridge cup embedded electronic provincial competition

Preliminary understanding of float

剑指 Offer 35. 复杂链表的复制

Two years of crud, two graduates, two months of preparation for the interview with ALI, and fortunately won the offer grading p6
随机推荐
OCR knowledge summary
Question of hanging the interviewer
GKConstantNoiseSource
盘点:144个免费学习网站,全网最全资源合集
If you don't climb mountains, you don't know the height of the sky; If you don't face deep streams, you don't know the thickness of the earth
ctf技能树----文件上传
The blogs of excellent programmers at home and abroad are all here, please check it
吊打面试官的问题
Sword finger offer 35. replication of complex linked list
samba服务器配置
Select without the order by clause, the order of the returned results is not reliable
Blue Bridge Cup embedded Hal library USART_ TX
Problems needing attention when VC links static libraries
剑指 Offer 06. 从尾到头打印链表
Redis-day01 common sense supplement and redis introduction
Tensorflow knowledge points
Samba server configuration
Andorid 开发三 (Intent)
5. Implement MapReduce program on window side to complete wordcount function
Andorid development III (intent)