Created
April 19, 2016 14:41
-
-
Save JanGorman/8f683732eadf0cc8630b3c1e09eeffbe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func toInt24(value: Int) -> NSData { | |
| var result = [UInt8]() | |
| result.append(UInt8(value >> 16)) | |
| result.append(UInt8(value >> 8)) | |
| result.append(UInt8(value)) | |
| return NSData(bytes: result, length: result.count) | |
| } | |
| func int24FromData(data: NSData, inout range: NSRange) -> Int { | |
| let offset = sizeof(UInt8) | |
| var read = -1 | |
| data.getBytes(&read, range: range) | |
| var result = (read & 0xff) << 16 | |
| range.location += offset | |
| data.getBytes(&read, range: range) | |
| result |= (read & 0xff) << 8 | |
| range.location += offset | |
| data.getBytes(&read, range: range) | |
| result |= read & 0xff | |
| range.location += offset | |
| return result | |
| } | |
| let data = NSMutableData(data: toInt24(100)) | |
| data.appendData(toInt24(50)) | |
| var range = NSRange(location: 0, length: sizeof(UInt8)) | |
| let first = int24FromData(data, range: &range) | |
| let second = int24FromData(data, range: &range) |
Author
It is much appreciated 🙇 The stream comes with mixed types (24 bit ones, 32 bit ones as well as strings) in it. But could extract sub ranges for expected areas and work on those in the simplified fashion
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Didn't think about it much, but maybe it will give you some insight to start