An open API service indexing awesome lists of open source software.

https://github.com/vimpunk/endian

Ultra light-weight endian utility for C++11 (header only)
https://github.com/vimpunk/endian

Last synced: about 1 year ago
JSON representation

Ultra light-weight endian utility for C++11 (header only)

Awesome Lists containing this project

README

          

# Endian

A small, but much needed utility library for any program that needs to handle numbers during network or file IO.

## Usage

This is a header-only library, so just place `endian.hpp` in your system wide or project's include directory. Then, use the library by including endian, i.e.:
```c++
#include
```

### Working with buffers

Convert from Network Byte Order to Host Byte Order, e.g. when using a receive buffer.
```c++
std::vector buffer;
// ... receive data into buffer
const int64_t i = endian::read(buffer.data());
```

Convert from Host Byte Order to Network Byte Order, e.g. when using a send buffer.
```c++
std::vector buffer;
const int16_t number = 42;
endian::write(number, buffer.data());
```

You can also read and parse arbitrary width integers in the range [1, 8] specified in bytes .
```c++
const uint32_t three_bytes = 0xaabbcc;
endian::write(number, buffer.data());
auto res = endian::read(buffer.data());
assert(res = three_bytes);
```

There are also aliases provided:
```c++
endian::write_le<3>(number, buffer.data());
endian::write_be<3>(number, buffer.data());
endian::write_le(number, buffer.data());
endian::write_be(number, buffer.data());

number = endian::read_le<5>(buffer.data());
number = endian::read_be<5>(buffer.data());
number = endian::read_le(buffer.data());
number = endian::read_be(buffer.data());
```

### Platform specific functions

Note that these functions are only available if you're on one of the supported
platforms (where host byte order could be determined). As such, the following
functions are conditionally enabled with a preprocessor flag.

#### Reversing byte order

```c++
const int16_t a = 0x1234;
const int16_t b = endian::reverse(a);
// b is 0x3412
```

Alternatively, only reverse byte order if target endianness and host's endianness differ.
```c++
const auto i = endian::conditional_reverse(42);
```

#### ntoh, hton

Convenience function to conditionally convert to Network Byte Order, same as the POSIX hton function.
```c++
const auto n = endian::host_to_network(h);
```

Convenience function to conditionally convert from Network Byte Order, same as the POSIX ntoh function.
```c++
const auto h = endian::network_to_host(n);
```