GithubHelp home page GithubHelp logo

aleksey-nikolaev / endianness Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 25 KB

Endianness is very simple, light, header-only library designed to reduce conversions between big-endian and little-endian data

License: MIT License

CMake 10.40% C++ 89.60%
endianness endian cpp cpp17 cmake header-only big-endian little-endian

endianness's Introduction

Endianness

Endianness is very simple, light, header-only library designed to reduce conversions between big-endian and little-endian data.

  • Requires STL C++17

Build and Install

Usage after install

Endianness library can be built and installed as any other cmake project. After that add to your CMake project file CMakeLists.txt:

find_package(Endianness)

And link to your target imported target ENDIAN::Endian

target_link_libraries(<target> ENDIAN::Endian)

Usage as submodule

If you prefer submodule instead of install add to your CMakeLists.txt

add_subdirectory(<Endianness source realative path>)
target_link_libraries(<target> ENDIAN::Endian)

Debug Visualisation

With happy debugging on MSVS you can use Endian.natvis This natvis file can swap bytes of big-endian data for better view.

Usage

  • You can use the Endian in constexpr
  • It is possible to use the Endian as a key in unordered associative containers. Avoid to use with containers that need the less operator for comparision - only == and != operators have no byte swap.
  • Not necessary, but useful aliases:
#include <endianness.h>
#include <stdint.h>

using int16le = endianness::LittleEndian<int16_t>;
using uint16le = endianness::LittleEndian<uint16_t>;
using int32le = endianness::LittleEndian<int32_t>;
using uint32le = endianness::LittleEndian<uint32_t>;
using int64le = endianness::LittleEndian<int64_t>;
using uint64le = endianness::LittleEndian<uint64_t>;

using int16be = endianness::BigEndian<int16_t>;
using uint16be = endianness::BigEndian<uint16_t>;
using int32be = endianness::BigEndian<int32_t>;
using uint32be = endianness::BigEndian<uint32_t>;
using int64be = endianness::BigEndian<int64_t>;
using uint64be = endianness::BigEndian<uint64_t>;

Example

  • No byte swap type conversion:
constexpr uint32be MASK = 0xF0F01234; //Compile-time expressions
#pragma pack(push, 1)
struct FileHeader
{
    static constexpr uint32be SIGNATURE = 'A123'; //Compile-time expressions
    uint32be signature;
    uint32be tag; 
};
#pragma pop()
...
std::unordered_set<uint32be> tags{'qwer','tyui','opas','dfgh'};
std::unordered_map<uint32be, int> hashmap;
auto fileHeader = reinterpret_cast<const FileHeader*>(pointer);
if (fileHeader && fileHeader->signature == FileHeader::SIGNATURE){
  uint32be tag = fileHeader->tag;
  if(tags.count(tag)){
    tag &= MASK;
    ++hashmap[tag];
  }
}
  • With byte swap:
// no byte swap on big-endian platform
uint16be a = 7;
uint32be b = a;// uint16be -> uint16_t -> uint32_t -> uint32be
int c = a + b;
bool d = (a < 32) // ok: uint16be -> uint16_t : d = true
//bool e = (a != 3) // error: need explicitly cast a to int or 3 to uint16be
bool e = (a != (uint16be)3) // ok: e = true

endianness's People

Contributors

aleksey-nikolaev avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.