Latest 1.0
Homepage https://github.com/alexaubry/BytesKit
License MIT
Platforms ios 8.0, osx 10.10, watchos 2.0, tvos 9.0
Frameworks Foundation
Authors

import BytesKit

Swift 3.0 Licence Build Status Carthage compatible CocoaPods

BytesKit allows you to manipulate bytes in Swift objects in a protocol-oriented manner.

Features

  • [x] Protocol-oriented
  • [x] Object to array of bytes conversion
  • [x] Object to hex string conversion
  • [x] Hex string to array of bytes conversion
  • [x] Hex string to Data conversion
  • [x] Hex string validation

Platforms

  • [x] iOS 8.0 and above
  • [x] macOS 10.10 and above
  • [x] tvOS 9.0 and above
  • [x] watchOS 2.0 and above
  • [x] Linux (Swift 3)

Installation

Swift Package Manager

Add this line to your Package.swift file (in the dependencies array) :

.Package(url: "https://github.com/alexaubry/BytesKit.git", majorVersion: 1)

CocoaPods

Add this line to your Podfile :

pod 'BytesKit' ~> '1.0'

Carthage

Add this line to your Cartfile :

github 'alexaubry/BytesKit'

Manually

Drop the BytesKit.swift file into your project.

Usage

Protocol-orientation

This library defines the BytesConvertible protocol, which is the root of all of its features.

String and Data conform to this protocol out of the box, and you can make your own custom types conform to it as well.

Convert an object to an array of bytes

Objects that conform to the former protocol expose the bytes property, which returns the receiver converted to an array of bytes.

let data = Data(bytes: [0x1a, 0x2b, 0x3c, 0x4d])
let string = "ABCD"

let dataBytes = data.bytes // returns [0x1a, 0x2b, 0x3c, 0x4d]
let stringBytes = string.bytes // returns [0x41, 0x42, 0x43, 0x44]

Convert an object to a hex string

You can get the hex representation of an object that conform to the protocol using the hexString property :

let dataHexString = data.hexString // returns "1a2b3c4d"
let stringHexString = string.hexString // returns "41424344"

Convert a hex string to an array of bytes

You can convert a hex string to an array of bytes by calling Array<UInt8>.bytes(fromHexString:). The hex string will automatically be verified before the conversion.

let hexString = "A1B2C3"

if let bytes = Array<UInt8>.bytes(fromHexString: hexString) {
    print("Bytes : (bytes)")
} else {
    print("The hex string '(hexString)' is invalid.")
}

Convert a hex string to a Data object

Similarly, you can convert a hex string to a Data object using the init?(hexString:) initializer.

if let convertedData = Data(hexString: hexString) {
    print("Bytes : (convertedData.bytes)")
} else {
    print("The hex string '(hexString)' is invalid.")
}

Validate a hex string

To check if a string contains only HEX-compatible characters—that is 0123456789ABCDEFabcdef—, use the isValidHexString property.

let hex1 = "ABCDEF"
print(hex1.isValidHexString) // prints "true"

let hex2 = "ABCDEFG"
print(hex2.isValidHexString) // prints "false"

Latest podspec

{
    "name": "BytesKit",
    "version": "1.0",
    "summary": "A set of tools to interact with bytes in Swift",
    "description": "BytesKit allows you to manipulate bytes in Swift objects in a protocol-oriented manner. It provides utility features that allow you to convert objects to arrays of bytes, to get their hex strings and to convert hex strings to arrays of bytes or Data objects. This library supports String and Data out of the box, and you can extend it with your own types through protocol conformance.",
    "homepage": "https://github.com/alexaubry/BytesKit",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Alexis Aubry Radanovic": "[email protected]"
    },
    "social_media_url": "http://twitter.com/leksantoine",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "watchos": "2.0",
        "tvos": "9.0"
    },
    "source": {
        "git": "https://github.com/alexaubry/BytesKit.git",
        "tag": "1.0"
    },
    "source_files": "Sources",
    "frameworks": "Foundation",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This