惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
B
Blog
D
Docker
C
Check Point Blog
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
MongoDB | Blog
MongoDB | Blog
WordPress大学
WordPress大学
Jina AI
Jina AI
罗磊的独立博客
月光博客
月光博客
博客园 - Franky
L
LangChain Blog
H
Help Net Security
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
小众软件
小众软件
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

楚天乐的全栈之路

procrastinated单词终极指南:发音/中文解释/英文解释/词源/例句全解 | myxtea学英语 - 楚天乐的全栈之路 Vocabulary: Prolific - 楚天乐的全栈之路 DCT离散余弦变换和JPEG 压缩算法 - 楚天乐的全栈之路 地图找出口算法python实现 - 楚天乐的全栈之路 使用Vault管理服务器各种密码 - 楚天乐的全栈之路 windows环境pip无法安装dlib库的终极解决 - 楚天乐的全栈之路 Windows WLS2使用本机ss代理访问github - 楚天乐的全栈之路 写个dockerfile自动部署hugo - 楚天乐的全栈之路 php中__METHOD__和_FUNCTION__的区别 - 楚天乐的全栈之路
c语言float和bytes array转换 - 楚天乐的全栈之路
2024-04-29 · via 楚天乐的全栈之路

重点:大小端不要搞错

  1. x86是小端,0x123456,对应4个字节,0x12,0x34,0x56,0x78. 在内存中的存储顺序是 0x78, 0x56, 0x34, 0x12
#include <stdio.h>

int main(){
    unsigned char buffer[] = {0x46, 0x87, 0x92, 0x64};
    float a = 17353.195f;
    float b = 0;

    printf("zzzzz ....\n");

    unsigned int* pa = (unsigned int*)(&a);

    unsigned int x = *pa;
    int x1 = (x >> 24) & 0xFF;
    int x2 = (x >> 16) & 0xFF;
    int x3 = (x >> 8) & 0xFF;
    int x4 = x  & 0xFF;

    printf("%x",x1 );
    printf("%x", x2);
    printf("%x", x3);
    printf("%x", x4);
    printf("\n");

    unsigned char* pb = (unsigned char*)(&b);

    // 重点!
    *pb = buffer[3];
    *(pb+1) = buffer[2];
    *(pb+2) = buffer[1];
    *(pb+3) = buffer[0];

    printf("%.8f\n", b);
    return 1;
}

输出

46879264
17353.19531250