zoukankan      html  css  js  c++  java
  • 计算机编码--c语言中输出float的十六进制和二进制编码

      c语言中没有可以直接打印float类型数据的二进制或者十六进制编码的输出格式,

    因此,需要单独给个函数,如下:

     1 unsigned int float2hexRepr(float* a){
     2     unsigned int c;  
     3     c= ((unsigned int*)a)[0];   
     4     return c;
     5 }
     6 
     7 int main(int argc, char const *argv[])
     8 {
     9     printf("%s
    ", "== in float representation == ");
    10     float f1 = 15213.0;
    11     printf("%x
    ", float2hexRepr(&f1));
    12 }

    结果如下:

    1 == in float representation ==
    2 466db400

    为了更好看,打印出二进制:

     1 void hex2binaryStr(unsigned int x, char* str){
     2     unsigned int xCopy = x;
     3     for (int i = 0; i < 32; ++i)
     4     {
     5         str[31 - i] = (xCopy & 1)? '1': '0';
     6         xCopy = xCopy >> 1;
     7     }
     8 }
     9 
    10 void printBinary(char* str){
    11     for (int i = 0; i < 32; ++i)
    12     {
    13         printf("%c", str[i]);
    14         if (((1+i)%4 == 0) && ((1+i) != 32))
    15         {
    16             printf("%c", ',');
    17         }
    18     }
    19     printf("
    ");
    20 }

    结果如下:

    1 == in float representation ==
    2 466db400
    3 0100,0110,0110,1101,1011,0100,0000,0000

    THE END

  • 相关阅读:
    Scrapy中间件
    Scrapy简介
    Scrapy解析器xpath
    postman
    yarn
    brew 安装 yarn 时候失败
    immutability-helper 用途+使用方法
    js 正则
    react redux 应用链接
    react 事件传参数
  • 原文地址:https://www.cnblogs.com/tlz888/p/9211600.html
Copyright © 2011-2022 走看看