浮点数十六进制互转
发布日期:2021-05-10 05:07:27 浏览次数:15 分类:精选文章

本文共 4156 字,大约阅读时间需要 13 分钟。

C���������������������������������������������������

���������������������C���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������union������C������������������������������������

���������������������������

������������������������������������������������������union packet���������������

union packet {
float a;
char b[4];
};

���������union packet���������������������������������������������������������

  • float a;���������������������
  • char b[4];������������������������������������

������������������������������������������������������������������������float���������������������������������������������������������������������������������


���������������������������������������������

������������������������������������������������������������������������������������������������

float hex_to_float(unsigned char array[] size) {
unsigned int i = 0;
union packet p;
p.b[i] = array[i]; // ���������������������������������������������������������
// ���������������������������������������������������
return p.a; // ���������������������������������������������
}

���������������������array������������������������������������������������������������������������������������������union packet���b���������������������������������������������float���������


���������������������������������������������

���������������������������������������������������������������������������������������������������

void float_to_hex(unsigned char array[4], float f32_value) {
unsigned int i = 0;
union packet p;
p.a = f32_value; // ������������������������������������
// ������������������������`char`���������������������
// ���������`array`���������
_memcpy(array, p.b, sizeof(array)); // ������`memcpy`������������������������
}

���������������������������������������union packet���float������������������char������������������������������������array������


������������������������

������������������C���������������

#include 
/* ������������������������ */
union packet {
float a;
char b[4];
};
/* ������������������������������������ */
float hex_to_float(unsigned char array[4]) {
unsigned int i = 0;
union packet p;
for (; i < 4; i++)
p.b[i] = array[i];
return p.a;
}
/* ������������������������������������ */
void float_to_hex(unsigned char array[4], float f32_value) {
unsigned int i = 0;
union packet p;
p.a = f32_value;
for (; i < 4; i++)
array[i] = p.b[i];
}
int main() {
/* ��������������������������������������������������������� */
unsigned char hex_data[4] = {0x9a, 0x99, 0x19, 0x40};
float number;
number = hex_to_float(hex_data);
printf("������������������������������������������%f\n", number);
/* ������������������������������������������������������������ */
unsigned char hex_buffer[4];
float_to_hex(hex_buffer, number);
printf("������������������������������������������0x%02x 0x%02x 0x%02x 0x%02x\n"
, hex_buffer[0], hex_buffer[1], hex_buffer[2], hex_buffer[3]);
return 0;
}

���������������������������������

  • ������������

    • ������������������#include <stdio.h>���������������������/������������������
    • ������������������������������union packet���������������������������������������������������������������������������
    • ���������������������hex_to_float���float_to_hex������������������������������������������������������������������
    • ���������main()���
      • ���������������������������������������������������������������������������������������
      • ���������������������������������������������������������������������������
  • ������������

    • ������������������������������������������������������������������������������������
    • ���������������������������������������������������������������

  • ������������

    ������������C������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

    上一篇:stm系列单片机精准延时 systick NOP DWT
    下一篇:sqlite3 收益统计sql语句测试记录

    发表评论

    最新留言

    路过按个爪印,很不错,赞一个!
    [***.219.124.196]2025年04月06日 22时42分29秒