mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
19 lines
588 B
Swift
19 lines
588 B
Swift
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
import Foundation
|
|
|
|
extension String {
|
|
public func dataFromHex() -> Data? {
|
|
guard (self.count % 2) == 0 else { return nil }
|
|
|
|
let chars = self.map { $0 }
|
|
let bytes: [UInt8] = stride(from: 0, to: chars.count, by: 2)
|
|
.map { index -> String in String(chars[index]) + String(chars[index + 1]) }
|
|
.compactMap { (str: String) -> UInt8? in UInt8(str, radix: 16) }
|
|
|
|
guard (self.count / bytes.count) == 2 else { return nil }
|
|
|
|
return Data(bytes)
|
|
}
|
|
}
|