int8_t result ; [**self** getBytes:&result range:NSMakeRange(*offset, **sizeof**(result))] ; *offset += **sizeof**(result) ; **return** result ; - (uint8_t)eeo_readUint8:(**int** *)offset {     uint8_t result ; [**self** getBytes:&result range:NSMakeRange(*offset, **sizeof**(result))] ; *offset += **sizeof**(result) ; **return** result ; - (uint16_t)eeo_readUint16:(**int** *)offset {     uint16_t result ; [**self** getBytes:&result range:NSMakeRange(*offset, **sizeof**(result))] ; *offset += **sizeof**(result) ; **return** result ; - (uint16_t)eeo_readUint16ByBig:(**int** *)offset {     uint16_t result = [**self** eeo_readUint16: off set] ; result = CFSwapInt16HostToBig(result) ; **return** result ; - (**int**)eeo_readInt:(**int** *)offset {     **int** result ; [**self** getBytes:&result range:NSMakeRange(*offset, **sizeof**(result))] ; *offset += **sizeof**(result) ; **return** result ; - (**int**)eeo_readIntByBig:(**int***)offset {     **int** result ; [**self** getBytes:&result range:NSMakeRange(*offset, **sizeof**(result))] ; result = CFSwapInt32HostToBig(result) ; *offset += **sizeof**(result) ; **return** result ; - (uint32_t)eeo_readUint32:(**int***)offset {     uint32_t result ; [**self** getBytes:&result range:NSMakeRange(*offset, **sizeof**(result))] ; *offset += **sizeof**(result) ; **return** result ; - (uint32_t)eeo_readUint32ByBig:(**int***)offset {     uint32_t result = [**self** eeo_readUint32: off set] ; result = CFSwapInt32HostToBig(result) ; **return** result ; - (int64_t)eeo_readInt64:(**int** *)offset {     int64_t result ; [**self** getBytes:&result range:NSMakeRange(*offset, **sizeof**(result))] ; *offset += **sizeof**(result) ; **return** result ; - (uint64_t)eeo_readUint64:(**int** *)offset {     uint64_t result ; [**self** getBytes:&result range:NSMakeRange(*offset, **sizeof**(result))] ; *offset += **sizeof**(result) ; **return** result ; - (uint64_t)eeo_readUint64ByBig:(**int** *)offset {     uint64_t result ; [**self** getBytes:&result range:NSMakeRange(*offset, **sizeof**(result))] ; result = CFSwapInt64HostToBig(result) ; *offset += **sizeof**(result) ; **return** result ; - (NSNumber*)eeo_readUint64ToNumber:(**int***)offset {     uint64_t result = [**self** eeo_readUint64: off set] ; **return** @(result) ; - (NSNumber*)eeo_readUint64ByBigToNumber:(**int***)offset {     uint64_t result = [**self** eeo_readUint64ByBig: off set] ; **return** @(result) ; - (float_t)eeo_readFloat:(**int***)offset {     float_t result ; [**self** getBytes:&result range:NSMakeRange(*offset, **sizeof**(result))] ; *offset += **sizeof**(result) ; **return** result ; - (float_t)eeo_readFloatByBig:(**int** *)offset {     int32_t littleBytes ; [**self** getBytes:&littleBytes range:NSMakeRange(*offset, **sizeof**(littleBytes))] ; int32_t bigBytes = CFSwapInt32HostToBig(littleBytes) ; NSData * numData = [NSData dataWithBytes:&bigBytes length:**sizeof**(bigBytes)] ; float_t result ; [numData getBytes:&result length:**sizeof**(result)] ; *offset += **sizeof**(result) ; **return** result ; - (double_t)eeo_readDouble:(**int***)offset {     double_t result ; [**self** getBytes:&result range:NSMakeRange(*offset, **sizeof**(result))] ; *offset += **sizeof**(result) ; **return** result ; - (double_t)eeo_readDoubleByBig:(**int***)offset {     int64_t littleBytes ; [**self** getBytes:&littleBytes range:NSMakeRange(*offset, **sizeof**(littleBytes))] ; int64_t bigBytes = CFSwapInt64HostToBig(littleBytes) ; NSData * numData = [NSData dataWithBytes:&bigBytes length:**sizeof**(bigBytes)] ; double_t result ; [numData getBytes:&result length:**sizeof**(result)] ; *offset += **sizeof**(result) ; **return** result ; - (NSString*)eeo_readUTF8String:(**int** *)offset {     NSString * result = **nil** ; NSData * laterData = [**self** subdataWithRange:NSMakeRange(* off set, **self**.length-* off set)] ; **__block** NSUInteger strTerminatorIndex = 0 ; [laterData enumerateByteRangesUsingBlock:^(**const** **void** * **_Nonnull** bytes, NSRange byteRange, **BOOL** * **_Nonnull** stop) {         **for** (NSInteger i=0; i<byteRange.length; i++) {             **if**(((**char***)bytes)[i] == '\0'){                 * stop = ** YES ** ; **break** ; strTerminatorIndex ++ ; result = [[NSString alloc] initWithData:[laterData subdataWithRange:NSMakeRange( 0 , strTerminatorIndex)] encoding:NSUTF8StringEncoding] ; *offset += strTerminatorIndex + 1 ;//跳过结尾符占用的一个字节 **return** result ; - (NSString*)eeo_readUTF8String:(**int** *)offset stringLength:(**int**)length {     NSString * result = **nil** ; NSData * laterData = [**self** subdataWithRange:NSMakeRange(* off set, length)] ; result = [[NSString alloc] initWithData:laterData encoding:NSUTF8StringEncoding] ; *offset += length ; **return** result ; - (NSData *)eeo_readData:(**int** *)offset {     NSData * result = **nil** ; NSUInteger dataLength = [**self** eeo_readUint32ByBig: off set] ; **if** (**self**.length >= *offset + dataLength) { result = [**self** subdataWithRange:NSMakeRange(* off set, dataLength)] ; *offset += dataLength ; **return** result ; - (NSData *)eeo_readData:(**int** *)offset dataLength:(**int**)length {     NSData * result = **nil** ; result = [**self** subdataWithRange:NSMakeRange(* off set, length)] ; *offset += length ; **return** result ;

NSData数据写入分类按照约定的顺序写入

- (**void**)eeo_writeInt8:(int8_t)value {
    [**self** appendBytes:&value length:**sizeof**(int8_t)];
- (**void**)eeo_writeUint8:(uint8_t)value {
    [**self** appendBytes:&value length:**sizeof**(uint8_t)];
- (**void**)eeo_writeBoolean:(**BOOL**)value {
    [**self** appendBytes:&value length:**sizeof**(**BOOL**)];
- (**void**)eeo_writeUint16:(uint16_t)value {
    [**self** appendBytes:&value length:**sizeof**(uint16_t)];
- (**void**)eeo_writeUint16ByBig:(uint16_t)value {
    uint16_t bigValue = CFSwapInt16HostToBig(value);
    [**self** appendBytes:&bigValue length:**sizeof**(uint16_t)];
- (**void**)eeo_writeUshort:(ushort)value {
    [**self** appendBytes:&value length:**sizeof**(ushort)];
- (**void**)eeo_writeInt:(**int**)value {
    [**self** appendBytes:&value length:**sizeof**(**int**)];
- (**void**)eeo_writeIntByBig:(**int**)value {
    uint32_t bigValue = CFSwapInt32HostToBig(value);
    [**self** appendBytes:&bigValue length:**sizeof**(**int**)];
- (**void**)eeo_writeUint32:(uint32_t)value {
    [**self** appendBytes:&value length:**sizeof**(uint32_t)];
- (**void**)eeo_writeUint32ByBig:(uint32_t)value {
    uint32_t bigValue = CFSwapInt32HostToBig(value);
    [**self** appendBytes:&bigValue length:**sizeof**(uint32_t)];
- (**void**)eeo_writeInt64:(int64_t)value {
    [**self** appendBytes:&value length:**sizeof**(int64_t)];
- (**void**)eeo_writeUint64:(uint64_t)value {
    [**self** appendBytes:&value length:**sizeof**(uint64_t)];
- (**void**)eeo_writeUint64ByBig:(uint64_t)value {
    uint64_t bigValue = CFSwapInt64HostToBig(value);
    [**self** appendBytes:&bigValue length:**sizeof**(uint64_t)];
- (**void**)eeo_writeFloat:(float_t)value {
    [**self** appendBytes:&value length:**sizeof**(float_t)];
- (**void**)eeo_writeFloatByBig:(float_t)value {
    int32_t littleBytes;
    NSData *littleData = [NSData dataWithBytes:&value length:**sizeof**(littleBytes)];
    [littleData getBytes:&littleBytes length:**sizeof**(littleBytes)];
    int32_t bigBytes = CFSwapInt32HostToBig(littleBytes);
    [**self** appendBytes:&bigBytes length:**sizeof**(bigBytes)];
- (**void**)eeo_writeDouble:(double_t)value {
    [**self** appendBytes:&value length:**sizeof**(double_t)];
- (**void**)eeo_writeDoubleByBig:(double_t)value {
    int64_t littleBytes;
    NSData *littleData = [NSData dataWithBytes:&value length:**sizeof**(littleBytes)];
    [littleData getBytes:&littleBytes length:**sizeof**(littleBytes)];
    int64_t bigBytes = CFSwapInt64HostToBig(littleBytes);
    [**self** appendBytes:&bigBytes length:**sizeof**(bigBytes)];
- (**void**)eeo_writeStringByUTF8:(NSString *)value {
    NSData *valueData = [value dataUsingEncoding:NSUTF8StringEncoding];
    [**self** appendData:valueData];
    **char** strTail = '\0';
    [**self** appendBytes:&strTail length:**sizeof**(strTail)];
- (**void**)eeo_writeStringByUTF8:(NSString*)value length:(NSUInteger)length {
    NSData *valueData = [value dataUsingEncoding:NSUTF8StringEncoding];
    **if**(valueData.length >= length){
        valueData = [valueData subdataWithRange:NSMakeRange(0, length)];
    [**self** appendData:valueData];
    **char** strTail = '\0';
    [**self** appendBytes:&strTail length:**sizeof**(strTail)];
- (**void**)eeo_writeStringByUTF16:(NSString*)value {
    NSData *valueData = [value dataUsingEncoding:NSUTF16StringEncoding];
    [**self** appendData:valueData];
- (**void**)eeo_writeData:(NSData *)data {
    [**self** eeo_writeUint32ByBig:(uint32_t)data.length];
    [**self** appendData:data];
复制代码
分类:
iOS
标签: