A fast, safe, and efficient CBOR serialization library for Swift on any platform.
swiftpackageindex.com/thecoolwinter/CBOR/1.1.1/documentation/cbor
atproto
swift
cbor
1//
2// String+asHexData.swift
3// CBOR
4//
5// Created by Khan Winter on 9/1/25.
6//
7
8#if canImport(FoundationEssentials)
9import FoundationEssentials
10#else
11import Foundation
12#endif
13
14extension String {
15 func asHexData() -> Data {
16 guard self.count.isMultiple(of: 2) else {
17 fatalError()
18 }
19
20 let chars = self.map { $0 }
21 let bytes = stride(from: 0, to: chars.count, by: 2)
22 .map { String(chars[$0]) + String(chars[$0 + 1]) }
23 .compactMap { UInt8($0, radix: 16) }
24
25 guard self.count / bytes.count == 2 else { fatalError() }
26 return Data(bytes)
27 }
28}