Hi everybody,

is there a simple way to print a floating point number in its internal representation
(i.e. binary or hex) in C(C++)? Yet another question -- is there a linux utility to do the opposite thing with a binary file (i.e. choose a range of bits and convert it to a floating point number)?

Thanks.

Best regards,
Nick.

Dani AI

Generated

If you only need a portable, human-readable view of the value (not the raw bits), C and C++ already provide hex floating output. In C++11+, do std::cout << std::hexfloat << x; and in C99/C++11 printf("%a\n", x);. This renders a normalized mantissa and a base-2 exponent (e.g., 0x1.921fb6p+1 for 3.14159). See and %a.

To print the exact IEEE-754 bit pattern safely (no aliasing UB), use C++20 std::bit_cast and std::bitset. This works for both float and double and is endianness-accurate for your machine. Pre-C++20, prefer std::memcpy into a uint32_t/uint64_t.

#include <bit>
#include <bitset>
#include <cstdint>
#include <iostream>

template<class T>
void print_ieee_bits(T x) {
    if constexpr (sizeof(T) == 4) {
        std::uint32_t u = std::bit_cast<std::uint32_t>(x);
        std::cout << std::bitset<32>(u) << '\n';
    } else if constexpr (sizeof(T) == 8) {
        std::uint64_t u = std::bit_cast<std::uint64_t>(x);
        std::cout << std::bitset<64>(u) << '\n';
    }
}

For the reverse (turn bytes/bits in a file into a float), GNU od can decode directly: od -An -t f4 -v file.bin (use f8 for doubles). For precise offsets or endianness control, a tiny Python helper is flexible:

import struct
with open("file.bin","rb") as f:
    f.seek(OFFSET)          # byte offset to the start of the field
    b = f.read(4)           # 4 for float, 8 for double
    x, = struct.unpack("<f", b)  # use ">f" for big-endian
    print(x)

Note: results assume IEEE-754 and depend on host endianness. See struct for format codes.

Recommended Answers

All 5 Replies

Something like this?

Hi everybody,

is there a simple way to print a floating point number in its internal representation
(i.e. binary or hex) in C(C++)? Yet another question -- is there a linux utility to do the opposite thing with a binary file (i.e. choose a range of bits and convert it to a floating point number)?

Thanks.

Best regards,
Nick.

Here's a program that writes a float or double in the form 2^exp * mantissa, with the mantissa expanded in binary. IEEE specs might call for an offset of the exponent.

#include <iostream.h>

main()
{  double x, mant;  
    int exp = 0;
     cout << "Enter postive x: ";
      //Now find exponent and mantissa
      mant = x;
      while (mant >= 1)
      { mant /= 2;
         exp++;
           // At this point x = mant * 2^(exp)
     }
      while (mant < 0.5) 
      {     mant *= 2;
              exp--;
      }
      cout << x<<" is stored in the computer as \n"<< x <<" = ";
     cout <<"2^(" <<exp<<") *.";
     while (mant > 0)
     {   mant *= 2;
          if (mant >= 1)
         {  cout << '1';
            mant -= 1;
        }
            else
            cout << '0';
    }
          cout << endl;
}

I hope I haven't incorrectly entered this code. I have to find out how to upload a code from my hard disk.

cin statement is nwys missing......... :P

commented: Useless 4-year bump award -6

Here is a nice one :) ...

I know this is an old thread, but there is a simpler way of doing this I think:

#include <iostream>
#include <bitset>

void float_bin(float f)
{
    int x = *(int *)&f;
    std::bitset<sizeof(int) * 8> binary(x);
    std::cout << binary << std::endl;    
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.