C++ - serialize double to binary file in little en

2019-07-03 05:46发布

问题:

I'm trying to implement a function that writes double to binary file in little endian byte order.
So far I have class BinaryWriter implementation:

void BinaryWriter::open_file_stream( const String& path )
{
 // open output stream
  m_fstream.open( path.c_str(), std::ios_base::out | std::ios_base::binary);
  m_fstream.imbue(std::locale::classic());   
}

void BinaryWriter::write( int v )
{
  char data[4];
  data[0] = static_cast<char>(v & 0xFF);
  data[1] = static_cast<char>((v >> 8) & 0xFF);
  data[2] = static_cast<char>((v >> 16) & 0xFF);
  data[3] = static_cast<char>((v >> 24) & 0xFF);
  m_fstream.write(data, 4);
}

void BinaryWriter::write( double v )
{
   // TBD
}

void BinaryWriter::write( int v ) was implemented using Sven answer to What is the correct way to output hex data to a file? post.
Not sure how to implement void BinaryWriter::write( double v ).
I tried naively follow void BinaryWriter::write( int v ) implementation but it didn't work. I guess I don't fully understand the implementation.

Thank you guys

回答1:

You didn't write this, but I'm assuming the machine you're running on is BIG endian, otherwise writing a double is the same as writing an int, only it's 8 bytes.

const int __one__ = 1;
const bool isCpuLittleEndian = 1 == *(char*)(&__one__); // CPU endianness
const bool isFileLittleEndian = false;  // output endianness - you choose :)


void BinaryWriter::write( double v )
{
  if (isCpuLittleEndian ^ isFileLittleEndian) {
    char data[8], *pDouble = (char*)(double*)(&v);
    for (int i = 0; i < 8; ++i) {
      data[i] = pDouble[7-i];
    }
    m_fstream.write(data, 8);
  }
  else
    m_fstream.write((char*)(&v), 8);
}


回答2:

But don't forget generally int is 4 octects and double is 8 octets.

Other problem is static_cast. See this example :

double d = 6.1; char c = static_cast(d); //c == 6

Solution reinterpret value with pointer :

double d = 6.1;
char* c = reinterpret_cast<char*>(&d);

After, you can use write( Int_64 *v ), which is a extension from write( Int_t v ).

You can use this method with :

double d =  45612.9874
binary_writer.write64(reinterpret_cast<int_64*>(&d));

Don't forget size_of(double) depend of system.



回答3:

A little program converting doubles to an IEEE little endian representation. Besides the test in to_little_endian, it should work on any machine.

include <cmath>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits>
#include <sstream>
#include <random>

bool to_little_endian(double value) {

    enum { zero_exponent = 0x3ff };

    uint8_t  sgn = 0;      //  1 bit
    uint16_t exponent = 0; // 11 bits
    uint64_t fraction = 0; // 52 bits

    double d = value;

    if(std::signbit(d)) {
        sgn = 1;
        d = -d;
    }

    if(std::isinf(d)) {
        exponent = 0x7ff;
    }
    else if(std::isnan(d)) {
        exponent = 0x7ff;
        fraction = 0x8000000000000;
    }
    else if(d) {
        int e;
        double f = frexp(d, &e);

        // A leading one is implicit.
        // Hence one has has a zero fraction and the zero_exponent:
        exponent = uint16_t(e + zero_exponent - 1);

        unsigned bits = 0;
        while(f) {
            f *= 2;
            fraction <<= 1;
            if (1 <= f) {
                fraction |= 1;
                f -= 1;
            }
            ++bits;
        }
        fraction = (fraction << (53 - bits)) & ((uint64_t(1) << 52) - 1);
    }

    // Little endian representation.
    uint8_t data[sizeof(double)];
    for(unsigned i = 0; i < 6; ++i) {
        data[i] = fraction & 0xFF;
        fraction >>= 8;
    }
    data[6] = (exponent << 4) | fraction;
    data[7] = (sgn << 7) | (exponent >> 4);


    // This test works on a little endian machine, only.
    double result = *(double*) &data;
    if(result == value || (std::isnan(result) && std::isnan(value))) return true;
    else {
        struct DoubleLittleEndian {
            uint64_t fraction : 52;
            uint64_t exp : 11;
            uint64_t sgn : 1;
        };

        DoubleLittleEndian little_endian;
        std::memcpy(&little_endian, &data, sizeof(double));
        std::cout << std::hex
            << "  Result: " << result << '\n'
            << "Fraction: " << little_endian.fraction << '\n'
            << "     Exp: " << little_endian.exp << '\n'
            << "     Sgn: " << little_endian.sgn << '\n'
            << std::endl;

        std::memcpy(&little_endian, &value, sizeof(value));
        std::cout << std::hex
            << "   Value: " << value << '\n'
            << "Fraction: " << little_endian.fraction << '\n'
            << "     Exp: " << little_endian.exp << '\n'
            << "     Sgn: " << little_endian.sgn
            << std::endl;

        return false;
    }
}


int main()
{
    to_little_endian(+1.0);
    to_little_endian(+0.0);
    to_little_endian(-0.0);
    to_little_endian(+std::numeric_limits<double>::infinity());
    to_little_endian(-std::numeric_limits<double>::infinity());
    to_little_endian(std::numeric_limits<double>::quiet_NaN());

    std::uniform_real_distribution<double> distribute(-100, +100);
    std::default_random_engine random;
    for (unsigned loop = 0; loop < 10000; ++loop) {
        double value = distribute(random);
        to_little_endian(value);
    }
    return 0;
}