Skip to content

Commit b6d5ca9

Browse files
committed
feat: encode Data feature
1 parent eec0197 commit b6d5ca9

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Sources/SwiftEncoding/Base64.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import Foundation
2+
13
public enum Base64Error: Error {
24
case invalidBase64Char
35
}
46

7+
public func encodeBase64(data: Data) -> String { encodeBase64(data: data.bytes) }
8+
59
public func encodeBase64(data: String) -> String { encodeBase64(data: data.asByteArray()) }
610

711
public func encodeBase64(data: [UInt8]) -> String {

Sources/SwiftEncoding/Hex.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import Foundation
2+
13
public enum HexError: Error {
24
case invalidHexChar
35
}
46

7+
public func encodeHex(data: Data) -> String { encodeHex(data: data.bytes) }
8+
59
public func encodeHex(data: String) -> String { encodeHex(data: data.asByteArray()) }
610

711
public func encodeHex(data: [UInt8]) -> String {

Sources/SwiftEncoding/SwiftEncoding.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Foundation
2+
13
public extension String {
24
func asByteArray() -> [UInt8] {
35
return self.utf8.map{UInt8($0)}
@@ -6,4 +8,16 @@ public extension String {
68
var byteArray : [UInt8] {
79
return self.utf8.map{UInt8($0)}
810
}
11+
}
12+
13+
extension Data {
14+
var bytes: [UInt8] {
15+
return [UInt8] (self)
16+
}
17+
}
18+
19+
extension Array where Element == UInt8 {
20+
var data: Data {
21+
return Data(self)
22+
}
923
}

0 commit comments

Comments
 (0)