Sha256: 537aa5c3ece172623fed7519747b7baca723ade35716e477e166612b8182314c

Contents?: true

Size: 1.15 KB

Versions: 325

Compression:

Stored size: 1.15 KB

Contents

struct Hexadecimal {

    let hexString: String
    let intValue: Int?

    private static let hexDigits = [
        "0": 0,
        "1": 1,
        "2": 2,
        "3": 3,
        "4": 4,
        "5": 5,
        "6": 6,
        "7": 7,
        "8": 8,
        "9": 9,
        "a": 10,
        "b": 11,
        "c": 12,
        "d": 13,
        "e": 14,
        "f": 15
    ]

    init(_ hexString: String) {
        self.hexString = hexString
        self.intValue = Hexadecimal.getIntFromHexString(hexString)
    }

    private static func getIntFromHexString(_ hexString: String) -> Int? {
        var result = 0
        var multiplier = 1

        let digits = hexString.characters.map { String($0).lowercased() }.reversed()

        for digit in digits {
            guard let intValue = hexDigits[digit] else {
                // Invalid hex string
                return nil
            }

            result += intValue * multiplier
            multiplier *= 16
        }

        return result
    }
}

extension Int {

    init?(_ hex: Hexadecimal) {
        guard let intValue = hex.intValue else {
            return nil
        }

        self = intValue
    }

}

Version data entries

325 entries across 325 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.179 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.178 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.177 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.176 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.175 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.174 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.173 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.172 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.171 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.170 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.169 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.167 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.166 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.165 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.164 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.163 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.162 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.161 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift
trackler-2.2.1.160 tracks/swift/exercises/hexadecimal/Sources/HexadecimalExample.swift