当前位置:网站首页>C language to convert float data into BCD data
C language to convert float data into BCD data
2022-07-28 11:07:00 【tutu-hu】
One . sketch
Use BCD The encoding method can save byte space to a greater extent , Save a decimal number with four binary digits . This paper completes float Type data is converted to BCD Conversion of coding mode , And make the corresponding conversion according to the number of digits after the decimal point .
Two . Code implementation
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/** * @brief take float To bcd * * @param input: Input float Type data * decimal_num: The number of decimal places to keep * output_buf: Output buffer * output_num: Final conversion output bytes * * @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; // Most intermediate strbuf length
char string_buffer[20] = {
0}; // Define temporary cache
// Do different operations according to the reserved decimal places
switch (decimal_num)
{
case 0:
sprintf_s(string_buffer, "%.0f", input); // Will input float Type variable for formatted output
break;
case 1:
sprintf_s(string_buffer, "%.2f", input); // Will input float Type variable for formatted output
len_temp = strlen(string_buffer); // Get the formatted data length
memmove(string_buffer + len_temp - 3, string_buffer + len_temp - 2, 1); // Memory move , Can overlap
string_buffer[len_temp - 2] = 0; // Add Terminator
break;
case 2:// Two decimal places
sprintf_s(string_buffer, "%.3f", input); // Will input float Type variable for formatted output
len_temp = strlen(string_buffer); // Get the formatted data length
memmove(string_buffer + len_temp - 4, string_buffer + len_temp - 3, 2); // Memory move , Can overlap
string_buffer[len_temp - 2] = 0; // Add Terminator
break;
case 3:
sprintf_s(string_buffer, "%.4f", input); // Will input float Type variable for formatted output
len_temp = strlen(string_buffer); // Get the formatted data length
memmove(string_buffer + len_temp - 5, string_buffer + len_temp - 4, 3); // Memory move , Can overlap
string_buffer[len_temp - 2] = 0; // Add Terminator
break;
default:break;
}
len_final = strlen(string_buffer);
printf("len_final = %d string_buffer = %s\n", len_final, string_buffer);
//string To BCD Transformation
if (len_final % 2) // There are odd digits in total
{
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 // There are even digits
{
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; // Get the number of bytes finally generated
}
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); // Retain 0 Decimal place
//float_to_bcd(a, 1, bcd_buffer, &len_final); // Retain 1 Decimal place
//float_to_bcd(a, 2, bcd_buffer, &len_final); // Retain 2 Decimal place
float_to_bcd(a, 3, bcd_buffer, &len_final); // Retain 3 Decimal place
for (i = 0; i < len_final; i++)
{
printf("%x ", bcd_buffer[i]);
}
return 0;
}3、 ... and . Execution results
// Retain 0 Decimal place
len_final = 5 string_buffer = 12142
Output BCD data :01 21 42
// Retain 1 Decimal place
len_final = 6 string_buffer = 121420
Output BCD data :12 14 20
// Retain 2 Decimal place
len_final = 7 string_buffer = 1214201
Output BCD data :01 21 42 01
// Retain 3 Decimal place
len_final = 8 string_buffer = 12142009
Output BCD data :12 14 20 09边栏推荐
- 适合中小企业的进销存软件,搞定5大难题
- Pyqt5 rapid development and practice 4.13 menu bar, toolbar and status bar and 4.14 qprinter
- Yan reports an error: exception message: /bin/bash: line 0: fg: no job control
- 这里有一份超实用Excel快捷键合集(常用+八大类汇总)
- Jianzhi offer 09. realize queue with two stacks
- Usage of memory operation functions memcpy() and memmove()
- 2021-03-24
- samba服务器配置
- ctf技能树----文件上传
- 做数据分析,你还不懂RFM分析方法(模型)?
猜你喜欢

Learn these analysis methods and models, and no longer have no ideas when encountering problems

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

剑指 Offer 06. 从尾到头打印链表

Learn how to do e-commerce data analysis (with operation analysis index framework)

盘点:6本书教会你职场晋升必备技能

剑指 Offer 30. 包含min函数的栈

JS - 修改数组中对象的键名

I use the applet container to improve the efficiency of mobile R & D by 5 times!

为什么传输前要进行编码与调制

Relevant knowledge points of hash table
随机推荐
2021-03-24
Blue Bridge Cup embedded Hal library ADC
Fhwy workday schedule
Learn these analysis methods and models, and no longer have no ideas when encountering problems
使用共用体union及指针测试大小端
Problems needing attention when VC links static libraries
Sword finger offer 30. stack containing min function
数组相关的知识点
为什么传输前要进行编码与调制
Arduino Basics
RHEL 6.4 安装svn和apache
Stacks and queues
CGAL compilation error
offsetof宏与container_of宏分析详解
GKLinearCongruentialRandomSource
蓝桥杯嵌入式-HAL库-USART_RX
剑指 Offer 09. 用两个栈实现队列
The 10th Landbridge cup embedded electronic provincial competition
Installation points and precautions of split angle probe
platform驱动平台下,关于probe函数中,形参dev的“dev->dev.of_node;”的理解