Giter Site home page Giter Site logo

bitlib's Introduction

Bitlib 0️⃣1️⃣0️⃣1️⃣

A simple OCaml library for bit/byte operations. The purpose of this library is to provide aid to users writing binary files, NOT perform bitwise operations on objects in memory.

Example

    // Write a short int (2 bytes) to a binary file
    let bits = to_bits 512 in
    let padded = pad 16 bits in
    let bytes = to_bytes padded 2 in
    write_binary_file bytes "output.byte"

API

to_bits i

Converts an integer, i, to a list of bits.

Signature

    val to_bits : int -> int list

Example

    to_bits 5 => [1;0;1]

pad padding bits

Ensures bits is of length padding, left-padding the list with 0's as needed.

Signature

    val pad : int -> int list -> int list

Example

    padding 8 [1;0;1] => [0;0;0;0;0;1;0;1]

to_bytes bits n

Writes the bit sequence, bits, as a byte sequence of length n. This function assumes bytes are 8 bits.

Signature

    val to_bytes: int list -> int -> int list

Example

    to_bytes (padding 32 (to_bits 256)) 4 => [0;0;1;0]

little_endian_of_int i bytes

Converts an integer, i, to a list of bytes, bytes long, in little endian order.

Signature

    val little_endian_of_int : int -> int -> int list

Example

    little_endian_of_int 5 2 => [0;5]

big_endian_of_int i bytes

Converts an integer, i, to a list of bytes, bytes long, in big endian order.

Signature

    val big_endian_of_int : int -> int -> int list

Example

    big_endian_of_int 5 2 => [5;0]

write_binary_file bytes filename

Creates a binary file named filename and writes the content of bytes to the file.

Signature

    val write_binary_file : int list -> string -> unit

Example

    write_binary_file [0;5] "output.byte"

Contributing

Contributions to bitlib are greatly appreciated! ❤️

Please try to keep its implementation unassuming and configurable. 🙂

bitlib's People

Contributors

chrisnevers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  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.