Boost logo

Boost Users :

Subject: Re: [Boost-users] Convert big endian to little endian (floating point values)
From: Paolo Bolzoni (paolo.bolzoni.brown_at_[hidden])
Date: 2015-12-08 21:32:01


On Wed, Dec 9, 2015 at 7:18 AM, Martin Vymazal <martin.vymazal_at_[hidden]> wrote:
> What would be a possible approach to save the data without 'relying on the
> platform specific bit representation of primitive data types' as you put it?

What about using boost integer to find an unsigned integer (let's call
its type Lint) longer or equal than the float (type Float) you have
and make an union?

union Lint_Float_t {
  Lint store;
  Float read;
};
static_assert(sizeof(Lint) >= sizeof(Float), "");

To store you copy the float in the union, and save the integer. To
load you load the integer and copy from the union the float.

It is definitely not portable (reading and writing different types in
a union is at most implementation dependant) and you keep the original
bits, but it should work at least on machine of the same type.

Cheers,
Paolo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net