Git Product home page Git Product logo

binarytools's Introduction

BinaryTools

C++ classes for reading/writing binary data and some helper functions/classes. Based on C#'s BinaryReader and BinaryWriter.

BinaryReader & BinaryWriter

Classes which can read/write binary data to/from a file or memory buffer. Both have functions for the most common primitive types. Ex: uint32_t, int32_t, uint64_t, int64_t, float, double, etc. See BinaryReader.h and BinaryWriter.h for a full list. The constructor used determines whether the class reads from a file (the constructor provides a file path), or a memory region (it provides a memory address and size). They can also read and write entire structs to or from memory using ReadToMemory and WriteFromMemory, respectively.

Other helpers and included classes

  • Span<T>: A very simple wrapper around a fixed sized memory region used by ReadAllBytes. You must free the memory the span points to if it's heap allocated.
  • MemoryBuffer: A simple class which inherits std::streambuf. Used by BinaryReader/Writer when interacting with a memory buffer.
  • ReadAllBytes(const std::string& filePath): Function that reads all bytes from a file and returns them in a Span. Since it's using a span you must free the memory it returns once you're done with it.

Example

This example shows how to read/write files and in memory buffers using BinaryReader and BinaryWriter.

#include <BinaryReader.h>
#include <BinaryWriter.h>
#include <string>
#include <iostream>
#include <assert.h>

//You can specify data layouts with a struct and read/write them in one go
struct ExampleHeader
{
    uint32_t Signature = 0xF00F;
    uint32_t Version = 10;
    uint32_t Value0 = 1024;
    float Value1 = 45.2f;
    float Value2 = 800.9f;
};

int main()
{
    //Write test file to your project directory
    std::string testFilePath = "TestFile0.bin";

    //File writing
    {
        BinaryWriter writer(testFilePath);
        writer.WriteUint32(100);
        writer.WriteFloat(512.0f);
        writer.WriteNullTerminatedString("Hello binary!");
        //Write padding bytes to align to value. E.g. position = 13. After align(4), position = 16, the next multiple of 4.
        writer.Align(4);

        //Can also write whole structs/classes to files
        ExampleHeader header;
        writer.WriteFromMemory(&header, sizeof(ExampleHeader));
    }

    //File reading
    {
        BinaryReader reader(testFilePath); //Assumes that the file already exists
        assert(reader.ReadUint32() == 100);
        assert(reader.ReadFloat() == 512.0f);
        assert(reader.ReadNullTerminatedString() == "Hello binary!");
        reader.Align(4);

        //Can also read whole structs/classes from files
        ExampleHeader header;
        reader.ReadToMemory(&header, sizeof(ExampleHeader));
        assert(header.Signature == 0xF00F);
        assert(header.Version == 10);
        assert(header.Value0 == 1024);
        assert(header.Value1 == 45.2f);
        assert(header.Value2 == 800.9f);
    }

    //Reading from memory
    {
        uint32_t someBuffer[5] = { 256, 700, 12, 895, 5784 };
        BinaryReader reader2((char*)&someBuffer, 5 * sizeof(uint32_t));
        assert(reader2.ReadUint32() == 256);
        assert(reader2.ReadUint32() == 700);
        assert(reader2.ReadUint32() == 12);
        assert(reader2.ReadUint32() == 895);
        assert(reader2.ReadUint32() == 5784);
    }
}

binarytools's People

Contributors

moneyl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

binarytools's Issues

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.