Code Monkey home page Code Monkey logo

endianness's Introduction

Endianness Macros and Functions for C and C++

Utilities for reading and writing multibyte values in big or little endian format.

Implemented as a header only (endianness.h).

Header Dependencies

  • stdint.h: For standard integer types
  • math.h: To test for __STDC_IEC_559__

Usage

#include <endianness/endianness.h>
#include <stdio.h>

static void demonstrate_endianness()
{
    uint8_t data[] = {0x12, 0x34};

    printf("Read [%02x %02x] in BE order: %02x %02x\n",
        data[0], data[1],
        data[INDEX_BE(sizeof(data), 0)],
        data[INDEX_BE(sizeof(data), 1)]);

    printf("Read [%02x %02x] in LE order: %02x %02x\n",
        data[0], data[1],
        data[INDEX_LE(sizeof(data), 0)],
        data[INDEX_LE(sizeof(data), 1)]);

    printf("Read [%02x %02x] as BE int: %04x\n", data[0], data[1],read_uint16_be(data));
    printf("Read [%02x %02x] as LE int: %04x\n", data[0], data[1],read_uint16_le(data));

    uint16_t value = 0x1234;

    write_uint16_be(value, data);
    printf("Write int %04x as BE: %02x %02x\n", value, data[0], data[1]);

    write_uint16_le(value, data);
    printf("Write int %04x as LE: %02x %02x\n", value, data[0], data[1]);

    // These will always give the same result, regardless of host endianness.
    uint8_t* ptr = (uint8_t*)&value;
    printf("Get from high byte of int %04x, index 0: %02x\n", value, ptr[INDEX_HB(sizeof(value), 0)]);
    printf("Get from high byte of int %04x, index 1: %02x\n", value, ptr[INDEX_HB(sizeof(value), 1)]);
    printf("Get from low byte of int %04x, index 0: %02x\n", value, ptr[INDEX_LB(sizeof(value), 0)]);
    printf("Get from low byte of int %04x, index 1: %02x\n", value, ptr[INDEX_LB(sizeof(value), 1)]);
}

Result:

Read [12 34] in BE order: 12 34
Read [12 34] in LE order: 34 12
Read [12 34] as BE int: 1234
Read [12 34] as LE int: 3412
Write int 1234 as BE: 12 34
Write int 1234 as LE: 34 12
Get from high byte of int 1234, index 0: 12
Get from high byte of int 1234, index 1: 34
Get from low byte of int 1234, index 0: 34
Get from low byte of int 1234, index 1: 12

Requirements

  • Meson 0.49 or newer
  • Ninja 1.8.2 or newer
  • A C compiler
  • A C++ compiler (for the tests)

Building

meson build
ninja -C build

Running Tests

ninja -C build test

For the full report:

./build/run_tests

Installing

ninja -C build install

License

Copyright 2019 Karl Stenerud

Released under MIT License https://opensource.org/licenses/MIT

endianness's People

Contributors

kstenerud avatar

Stargazers

Indragie Karunaratne avatar

Watchers

 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.