当前位置:网站首页>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边栏推荐
- JS - modify the key name of the object in the array
- 蓝桥杯嵌入式-HAL库-LCD
- Aike AI frontier promotion (7.28)
- Pyqt5 rapid development and practice 4.12 calendar and time
- JS - 修改数组中对象的键名
- nodejs:搭建express 服务,设置session以及实现退出操作
- Ten questions about low code: tell everything about low code!
- The Xiongguan pass is like an iron road, and now we are going to cross it from the beginning
- The solution of PHP sending mobile MAS SMS garbled code
- 读懂这6本书,学习MySQL更轻松
猜你喜欢

Purchase, sale and inventory software suitable for small and medium-sized enterprises to solve five major problems

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

Do you want to enroll in class for advanced soft exam

ICML 2022 | graph represents the structure aware transformer model of learning

Table data processing software, what else besides excel?

Blue Bridge Cup embedded Hal library USART_ TX

CTF skill tree - file upload

盘点:144个免费学习网站,全网最全资源合集

3. MapReduce explanation and source code analysis

21. Merge two ordered linked lists
随机推荐
Inventory: exciting data visualization chart
使用共用体union及指针测试大小端
02.1.2. logic type bool
Software designers ask 20 questions before the exam, pay attention!!
Install MySQL based on docker
Build a quick development ide: visualsvn + sublime + Visual Studio 2013 + quickeasyftpserver
CRM+零代码:轻松实现企业信息化
Usage of memory operation functions memcpy() and memmove()
ctf技能树----文件上传
JS - 修改数组中对象的键名
Table data processing software, what else besides excel?
Preliminary understanding of float
Sword finger offer 30. stack containing min function
Bc35 NB module at instruction development summary
Clo********e: project management notes
Purchase, sale and inventory software suitable for small and medium-sized enterprises to solve five major problems
几个数据库的相关概念
理解Oracle的几个概念
I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime
samba学习