[gdal-dev] Reading a double from file

Even Rouault even.rouault at spatialys.com
Tue Jun 16 02:17:00 PDT 2020


On mardi 16 juin 2020 11:02:55 CEST Mateusz Loskot wrote:
> On Tue, 16 Jun 2020 at 10:08, Andrew C Aitchison <andrew at aitchison.me.uk> wrote:
> > Does gdal have a standard, endian-independent,
> > way to read floating point values from file ?
> > Specifically IEEE-754 little-endian 64bit values.
> > 
> > So far my driver has used a union to "cast" 8 bytes to a double,
> > but I now discover that, whilst this is valid C, it is not valid C++.
> > I am looking for a portable way to read such values from file
> > into a C++ double. This cannot be the first gdal driver to need to do
> > this.
> 
> Generally, use std::memcpy or std::copy.
> Detect if you need to swap bytes and swap bytes.

A rather compact and portable way:

double x;
std::memcpy(&x, ..., sizeof(double));
CPL_LSBPTR64(&x); // if the ordering in the file is little-endian
CPL_MSBPTR64(&x); // if the ordering in the file is big-endian

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20200616/a58337ba/attachment.html>


More information about the gdal-dev mailing list