Giter VIP home page Giter VIP logo

bin's Introduction

Sysbot-bin

License Required PHP Version Latest Stable Version Tests Dependencies Code Quality

Sysbot/bin is a library used by Sysbot. It handles serialization and deserialization of binary data, providing support for both 32-bit and 64-bit systems.

Changelog

You can find the changelog here.

Installation

Install the library with composer:

$ composer require sysbot/bin --prefer-stable

(32-bit systems) You must also install the required dependency brick/math.

$ composer require "brick/math:^0.10"

Usage

Here's an example on how to use the library.

<?php

require_once 'vendor/autoload.php';

use Sysbot\Bin\Serializer;
use Sysbot\Bin\Deserializer;

// Serialization

$serializer = new Serializer();

$serializer->addLong(15); // adds a 32-bit integer (little-endian)

$long = PHP_INT_MAX;

if (PHP_INT_SIZE === 4) { // 32-bit systems
    // since 32-bit systems can't handle 64-bit numbers, we must relay on the BigInteger class
    $long = BigInteger::of('9223372036854775807');
}
$serializer->addLongLong($long, true); // adds a 64-bit integer (big-endian)

$serializer->addString('Hi mom!'); // adds a string

// casting a Serializer instance to a string will return the bytes
echo bin2hex((string)$serializer); // outputs "0f0000007fffffffffffffff074869206d6f6d21"


// Deserialization

$deserializer = new Deserializer((string)$serializer);

echo $deserializer->readLong(); // reads a 32-bit integer (little-endian), outputs "15"

$long = $deserializer->readLongLong(true); // reads a 64-bit integer (big-endian)

if (PHP_INT_SIZE === 4) {
    $long = (string)$long; // on 32-bit systems, an instance of the BigInteger class will be returned: to get the number, we must cast to string
}

echo $long; // outputs "9223372036854775807"

echo $deserializer->readString(); // reads a string, outputs "Hi mom!"

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.