Popularity
2.2
Growing
Activity
6.1
-
80
1
9

Programming language: Swift
Latest version: v3.0.1

BinaryKit alternatives and similar libraries

Based on the "Data Structures / Algorithms" category

Do you think we are missing an alternative of BinaryKit or a related project?

Add another 'Data Structures / Algorithms' Library

README

BinaryKit

Access bits and bytes directly in Swift.

Usage

Initialize from NSData

let data   = NSData(...)
let binary = Binary(data: data)

or [UInt8] bytes array

let binary = Binary(bytes: [0xDE, 0xAD]) // 1101 1110 1010 1101
// Read first 4 bits, bit by bit
var binary = Binary(bytes: [0xDE, 0xAD])
print(binary)

let bit0 = binary.next(bits: 1)
print(bit0) // 1

let bit1 = binary.next(bits: 1)
print(bit1) // 1

let bit2 = binary.next(bits: 1)
print(bit2) // 0

let bit3 = binary.next(bits: 1)
print(bit3) // 1
// Read next 4 bits, 2 x 2 bits
let bits4And5 = binary.next(bits: 2)
print(bits4And5) // 3

let bits6And7 = binary.next(bits: 2)
print(bits6And7) // 2
// Set reading offset (cursor) back to starting position
binary.readingOffset = 0
// Read first two bytes
let nextTwoBytes = binary.next(bytes: 2)
print(nextTwoBytes) // [222, 173]
// Read bit by position
let bit5 = binary.bit(5)
print(bit5) // 1
// Read byte by position
let byte1 = binary.byte(1)
print(byte1) // 173
// Read first 16 bits as Integer
let first16Bits = binary.bits(0, 16)
print(first16Bits) // 57005
// Read first two bytes as Integer
let firstTwoBytes = binary.bytes(0, 2) as Int
print(firstTwoBytes) // 57005

License

BinaryKit is released under the MIT License.


*Note that all licence references and agreements mentioned in the BinaryKit README section above are relevant to that project's source code only.