Convert between little-endian and big-endian floats effectively

孤街浪徒 提交于 2019-12-12 13:13:08

问题


I have a working software, which currently runs on a little-endian architecture. I would like to make it run in big-endian mode too. I would like to write little-endian data into files, regardless of the endianness of the underlying system.

To achieve this, I decided to use the boost endian library. It can convert integers efficiently. But it cannot handle floats (and doubles).

It states in the documentation, that "Floating point types will be supported in the Boost 1.59.0". But they are still not supported in 1.62.

I can assume, that the floats are valid IEEE 754 floats (or doubles). But their endianness may vary according to the underlying system. As far as I know, using the htonl and ntohl functions on floats is not recommended. How is it possible then? Is there any header-only library, which can handle floats too? I was not able to find any.

I could convert the floats to string, and write that into a file, I would like to avoid that method, for many reasons ( performance, disk-space, ... )


回答1:


Unheilig: you are correct, but

#include <boost/endian/conversion.hpp>


template <typename T>
inline T endian_cast(const T & t)
{
#ifdef BOOST_LITTLE_ENDIAN
    return boost::endian::endian_reverse(t);
#else
    return t;
#endif
}

or when u are using pointers, to immediate reversing, use:

template <typename T>
inline T endian_cast(T *t)
{
#ifdef BOOST_LITTLE_ENDIAN
    return boost::endian::endian_reverse_inplace(*t);
#else
    return t;
#endif
}

and use it, instead of manually (or maybe error-prone) reversing it's content

example:

std::uint16_t start_address() const
{
    std::uint16_t address;
    std::memcpy(&address, &data()[1], 2);
    return endian_cast(address);
}
void start_address(std::uint16_t i)
{
    endian_cast(&i);
    std::memcpy(&data()[1], &i, 2);
}

Good luck.




回答2:


Here:

float f = 1.2f;
auto it = reinterpret_cast<uint8_t*>(&f);
std::reverse(it, it + sizeof(f)); //f is now in the reversed endianness

No need for anything fancy.



来源:https://stackoverflow.com/questions/41220686/convert-between-little-endian-and-big-endian-floats-effectively

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!