Skip to content

choefele/CCHBinaryData

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 

CCHBinaryData

Utility classes for handling binary data, such as reading various data types from byte buffers.

Build Status Version Platform

See Releases for a high-level overview of recent updates.

Need to talk to a human? I'm @claushoefele on Twitter.

Installation

Use CocoaPods to integrate CCHBinaryData into your project. Minimum deployment targets are 7.0 for iOS and 10.9 for OS X.

platform :ios, '7.0'
pod "CCHBinaryData"
platform :osx, '10.9'
pod "CCHBinaryData"

Reading binary data

CCHBinaryDataReader can read various data types from byte buffers provided as NSData. The following sample code reads chunk information from a PNG image file:

    // Read PNG file. Data is big endian, see http://www.w3.org/TR/PNG/#7Integers-and-byte-order
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"pngbar" ofType:@"png"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    CCHBinaryDataReader *binaryDataReader = [[CCHBinaryDataReader alloc] initWithData:data options:CCHBinaryDataReaderBigEndian];
    
    // Skip file signature
    [binaryDataReader setNumberOfBytesRead:8];
    
    // Read chunks, see http://www.w3.org/TR/PNG/#5Chunk-layout
    while ([binaryDataReader canReadNumberOfBytes:4]) {
        unsigned int length = [binaryDataReader readUnsignedInt];
        NSString *chunkType = [binaryDataReader readStringWithNumberOfBytes:4 encoding:NSASCIIStringEncoding];
        [binaryDataReader skipNumberOfBytes:length];
        unsigned int crc = [binaryDataReader readUnsignedInt];
        
        [NSLog(@"%@ length: %tu, crc: 0x%08x\n", chunkType, length, crc];
    }