C++ Logo

STD-DISCUSSION

Advanced search

Subject: Re: What even happened to <net> and byte swapping?
From: Gennaro Prota (gennaro.prota_at_[hidden])
Date: 2021-02-24 10:12:17


On Fri, Feb 19, 2021 at 4:41 PM Matthew Woehlke via Std-Discussion
<std-discussion_at_[hidden]> wrote:
>
> I was looking at some code that needs to do byte-order swapping, and it
> seems the most portable way is still the non-standard htonX/ntohX family
> of functions.
>
> N3783 proposed standardizing these back in 2013(!)... but it doesn't
> seem that this ever happened. We got std::endian (finally) in C++20, but
> there do not seem to be any accompanying conversion functions.
>
> Am I missing something, or do we still not have a way in standard C++ to
> swap byte order of integer values?

I have worked on many applications that needed to handle different
endiannesses, but never had to swap bytes around, either in C++ or in
C#. Would something like this fit your needs:

  <https://github.com/gennaroprota/breeze/blob/master/breeze/endianness/endian_codec.hpp>

? The unit tests may give some usage hints, although everything should
be intuitive:

  <https://github.com/gennaroprota/breeze/blob/master/breeze/endianness/test/endian_codec_test.cpp>.

-- 
--
.:: Gennaro Prota ::.
.:: https://about.me/gennaro ::.

STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups