Swiftpack.co - spacenation/swift-binary-decoder as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by spacenation.
spacenation/swift-binary-decoder 2.0.0
:rocket: Binary Decoder
⭐️ 2
🕓 3 weeks ago
.package(url: "https://github.com/spacenation/swift-binary-decoder.git", from: "2.0.0")


SPM package to decode and encode types from binary data.

Binary Decoder

try? BinaryDecoder.decode(Message<DeliveryFlags>.self, from: [0b0000_1111, 0b1111_1111, 0b0000_0011])

Binary Encoder

try? BinaryEncoder.encode(Message(source: 15, data: Data([0b1111_1111]), flags: DeliveryFlags(isDelivered: true, isRead: true)))


private struct Message<Flags: Equatable & BinaryCodable>: Equatable, BinaryCodable {
    let source: UInt8
    let data: Data
    let flags: Flags
    init(source: UInt8, data: Data, flags: Flags) {
        self.source = source
        self.data = data
        self.flags = flags
    init(from decoder: BinaryDecoder) throws {
        source = try decoder.decode()
        data = try decoder.decode(size: .byte)
        try decoder.skip(size: 6)
        flags = try decoder.decode()
    func encode(to encoder: BinaryEncoder) throws {
        encoder.encode(data, size: .byte)
        encoder.encodeEmpty(size: 6)
        try encoder.encode(flags)

Code Contributions

Feel free to contribute via fork/pull request to master branch. If you want to request a feature or report a bug please start a new issue.


Stars: 2
Last commit: 3 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Release 2.0.0
3 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics