Skip to content

Instantly share code, notes, and snippets.

@sukov
Last active January 9, 2019 13:12
Show Gist options
  • Save sukov/2decc775db97d7231252cc8bc9a5cdd2 to your computer and use it in GitHub Desktop.
Save sukov/2decc775db97d7231252cc8bc9a5cdd2 to your computer and use it in GitHub Desktop.
Byte, Short Swift
import Foundation
extension Character {
var byte: UInt8 {
return String(self).utf8.map{UInt8($0)}[0]
}
var short: UInt16 {
return String(self).utf16.map{UInt16($0)}[0]
}
}
extension Sequence where Element == Character { // Same as String
var byteArray: [UInt8] {
return String(self).utf8.map{UInt8($0)}
}
var shortArray: [UInt16] {
return String(self).utf16.map{UInt16($0)}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment