Skip to content

A library for converting between `Data` and Hexadecimal (also known as Base-16) strings.

License

Notifications You must be signed in to change notification settings

yaslab/Hex.swift

Repository files navigation

Hex.swift

A library for converting between Data and Hexadecimal (also known as Base-16) strings.

Usage

Encode some Sequence<UInt8> to Hexadecimal string

// This is the original data.
let data: Data = "1234JKLM".data(using: .utf8)!

// Perform encoding.
let hexString: String = data.hexEncodedString()

print(hexString)
// -> "313233344a4b4c4d"

Decode Hexadecimal string to Data

// This is the Hexadecimal encoded string.
let hexString: String = "313233344a4b4c4d"

// Perform decoding.
let data: Data = Data(hexEncoded: hexString)!

print(String(decoding: data, as: UTF8.self))
// -> "1234JKLM"

Documentation

The documentation is available on Swift Package Index.

Installation

Swift Package Manager

Add the dependency to your Package.swift. For example:

// swift-tools-version: 5.8

import PackageDescription

let package = Package(
    name: "MyPackage",
    dependencies: [
        // Add `Hex.swift` package here.
        .package(url: "https://github.com/yaslab/Hex.swift.git", from: "1.0.0")
    ],
    targets: [
        .executableTarget(
            name: "MyCommand",
            dependencies: [
                // Then add it to your module's dependencies.
                .product(name: "Hex", package: "Hex.swift")
            ]
        )
    ]
)

License

Hex.swift is released under the MIT license. See the LICENSE file for more info.

About

A library for converting between `Data` and Hexadecimal (also known as Base-16) strings.

Topics

Resources

License

Stars

Watchers

Forks