Skip to content

Instantly share code, notes, and snippets.

@telliott99
Created December 14, 2015 02:31
Show Gist options
  • Select an option

  • Save telliott99/28b7ee6fe80b9c9bbe95 to your computer and use it in GitHub Desktop.

Select an option

Save telliott99/28b7ee6fe80b9c9bbe95 to your computer and use it in GitHub Desktop.
binary data class
import Foundation
func +(lhs: BinaryData, rhs: BinaryData) -> BinaryData {
return BinaryData(lhs.data + rhs.data)
}
class BinaryData : CustomStringConvertible, Indexable {
var data: [UInt8] = []
// we allow data to be empty
init(_ input: [UInt8] = []) {
data = input
}
convenience init(byteString b : ByteString) {
// allow spaces
assert (b.isValidByteString(spaces: true),
"found some bad characters in input bytes \"\(b)\"")
self.init(b.hexByteStringToIntArray())
}
var description : String {
get {
let sa = data.map { intToHexByte($0) }
return sa.joinWithSeparator("")
}
}
var count : Int {
get {
return self.data.count
}
}
var endIndex: Int {
get {
return data.count
}
}
var startIndex: Int {
get {
return data.count
}
}
subscript (position: Int) -> UInt8 {
get {
return data[position]
}
}
subscript (r: Range<Int>) -> BinaryData {
get {
var ret: [UInt8] = []
for (i,v) in data.enumerate() {
if r.contains(i) {
ret.append(v)
}
}
return BinaryData(ret)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment