当前位置:网站首页>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边栏推荐
猜你喜欢

Two years of crud, two graduates, two months of preparation for the interview with ALI, and fortunately won the offer grading p6

Blue Bridge Cup embedded Hal library LCD

CTF skill tree - file upload

什么是WordPress

21. Merge two ordered linked lists

BC35 NB模块AT指令开发总结

用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案

蓝桥杯嵌入式-HAL库-USART_RX

10_ UE4 advanced_ Add fall and cast actions

蓝桥杯嵌入式-HAL库-ADC
随机推荐
Nodejs: detect and install the NPM module. If it is already installed, skip
platform驱动平台下,关于probe函数中,形参dev的“dev->dev.of_node;”的理解
Install MySQL based on docker
判断数码管是共阳极还是共阴极
Build a quick development ide: visualsvn + sublime + Visual Studio 2013 + quickeasyftpserver
clo*******e:项目管理随记
盘点:144个免费学习网站,全网最全资源合集
Sword finger offer 30. stack containing min function
Select without the order by clause, the order of the returned results is not reliable
Jianzhi offer 09. realize queue with two stacks
Invalid ROM Table原因及解决办法
构建快捷开发IDE:VisualSVN+Sublime+Visual Studio 2013+QuickEasyFTPServer
Do data analysis, do you still not understand RFM analysis method (model)?
offsetof宏与container_of宏分析详解
Analysis of boot process of cortex-m4 and cortex-a7 kernel
Learn these analysis methods and models, and no longer have no ideas when encountering problems
Sword finger offer 06. print linked list from end to end
Array related knowledge points
nodejs:搭建express 服务,设置session以及实现退出操作
BOM部分属性及理解