Code Monkey home page Code Monkey logo

swift-byte-buffer's Introduction

Swift Byte Buffer

This framework provides a Swift implementation of 'ByteBuffer' which provides various tools for reading binary data. This includes support for bit fields.

Usage

ByteBuffer is a struct and reading/writing data mutates the internal state so it needs to be a var.

Reading Data

var buffer = ByteBuffer(data: data)

let bitField = try buffer.readBool(bits: 1)
let field2 = try buffer.readByte(bits: 3)
let field3 = try buffer.readByte(bits: 4)
let field4 = try buffer.readUInt64()

Calling rewind() resets the read position and allows you to start reading from the beginning of the buffer.

Writing Data

Data can be written as whole bytes or other data types written into bit increments. The resulting Data can be read out and easily converted to hex.

var buffer = ByteBuffer()
buffer.write(bytes: [0x01, 0x02, 0x03, 0x04])
buffer.write(short: 312, bits: 9)
buffer.write(byte: 128, bits: 7)
buffer.data.hexEncodedString()

Helpers

There are some extensions on Data to make working with hex data easier. See Data(hexEncoded:) and Data.hexEncodedString.

swift-byte-buffer's People

Contributors

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