Giter Club home page Giter Club logo

endian's Introduction

endian

endian is a collection of byte-order aware routines written in C. Unlike stream-oriented libraries that depend on a user-supplied method for reading and writing bytes, endian is optimized for reading from and writing between pointers in memory.

Sample Usage

#include "endian.h"
#include <stdio.h>

int main() {
  uint8_t bytes[] = {0x11, 0x22, 0x33, 0x44};
  printf("Interpreted as a little-endian byte stream, bytes = 0x%x\n", get_uint32_t_le((void *)bytes));
  printf("Interpreted as a big-endian byte stream, bytes = 0x%x\n", get_uint32_t_be((void *)bytes));
  return 0;
}

endian also provides two macros, MEMCPY_LE and MEMCPY_BE, which are ideal when you need to move data between two memory locations. They take the same arguments as memcpy() but will perform byte reversing as needed. The example above could be re-written as:

#include "endian.h"
#include <stdio.h>

int main() {
  uint8_t bytes[] = {0x11, 0x22, 0x33, 0x44};
  uint32_t v;
  
  MEMCPY_LE(&v, bytes, sizeof(uint32_t));
  printf("Interpreted as a little-endian byte stream, bytes = 0x%x\n", v);
  MEMCPY_BE(&v, bytes, sizeof(uint32_t));
  printf("Interpreted as a big-endian byte stream, bytes = 0x%x\n", v);
  return 0;
}

Unit Testing

endian includes built-in unit testing, which can be invoked as follows:

$ cc -Wall -DUNIT_TEST -o endian endian.c
$ ./endian
Starting endian tests...
...finished endian tests

Design

The read functions take a void *src pointer as an argument from which bytes are fetched. Similarly, the write functions take a void *dst pointer to specify where the converted data is to be stored.

Issues and pull requests are welcomed.

endian's People

Contributors

rdpoor avatar ipeperko avatar

Stargazers

Jo Wilkes avatar Ezequiel Haley avatar  avatar Ergative avatar  avatar Seth Girvan avatar Szczepan Zalega avatar Arturo Isaí Castro Pérpuli avatar

Watchers

James Cloos avatar  avatar  avatar  avatar

Forkers

ipeperko

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.