public byte[] toBytes() {
size = 12;
ByteBuffer buf = ByteBuffer.allocate(size);
buf.putInt(type.ordinal());//type is a enum
buf.putInt(id);
buf.putInt(size);
return buf.array();
}
@Override
public void fromBytes(byte[] data) {
ByteBuffer buf = ByteBuffer.allocate(data.length);
buf.put(data);
buf.rewind();
type = MessageType.values()[buf.getInt()];
id = buf.getInt();
size = buf.getInt();
}
I have two java methods and want to write an objective C method.. For the first method I wrote it into an Objective C code like
- (NSMutableData *) toBytes{
size = 12;
NSMutableData *buf = [[NSMutableData alloc] initWithCapacity:size];
NSData *dataType = [NSData dataWithBytes: &type length: sizeof(type)];
NSData *dataId = [NSData dataWithBytes: &msgId length: sizeof(msgId)];
NSData *dataSize = [NSData dataWithBytes: &size length: sizeof(size)];
[buf appendData:dataType];
[buf appendData:dataId];
[buf appendData:dataSize];
[dataType release];
[dataId release];
[dataSize release];
return buf;
}
But not sure how to read it back... It could've been easier if I add only one data into the buffer but I added total three data so I don't know how to read those back.. Thanks in advance...
Note to LCYSoft: i'm making this a community wiki. please correct any issues. i didn't compile this. since you posted one direction and really want an answer, i provided one. sorry, i am kinda busy atm.
this demonstrates both directions, and expands on the OP:
typedef enum t_mon_enum_type {
MONEnum_Edno = 1,
MONEnum_Dve = 2,
MONEnum_Tre = 3
} t_mon_enum_type;
@interface MONObject : NSObject
{
t_mon_enum_type type;
int msgId;
int size;
}
@end
@implementation MONObject
/* ... */
- (NSMutableData *)dataRepresentation
{
const int typeAsInt = (int)type;
const size_t capacity = sizeof(typeAsInt) + sizeof(msgId) + sizeof(size);
NSMutableData * data = [[NSMutableData alloc] initWithCapacity:capacity];
[data appendBytes:&typeAsInt length:sizeof(typeAsInt)];
[data appendBytes:&msgId length:sizeof(msgId)];
[data appendBytes:&size length:sizeof(size)];
return [data autorelease];
}
- (BOOL)isDataRepresentationValid:(NSData *)data { /* @todo */ }
- (BOOL)restoreFromDataRepresentation:(NSData *)data
{
if (![self isDataRepresentationValid]) {
return NO;
}
NSRange range = { 0, 0 };
int tmp = 0;
/* restore `type` */
range.length = sizeof(tmp);
[data getBytes:&tmp range:range];
type = (t_mon_enum_type)tmp;
/* advance read position */
range.location += range.length;
/* restore `msgId` */
range.length = sizeof(msgId);
[data getBytes:&msgId range:range];
/* advance read position */
range.location += range.length;
/*
setting the length here is redundant in this case, but it's how we
write it when dealing with more complex pod types.
*/
range.length = sizeof(size);
[data getBytes:&size range:range];
return YES;
}
i'm not going to rewrite the program for you, but i'll provide a tip:
you can use c++ in objc programs. specifically, you can compile as C (.c), ObjC (.m), C++ (.cpp), and ObjC++ (.mm). note: one common extension follows each language. the compiler will (by default) compile using the language implied by the file extension.
now, many java programs more closely resemble c++ programs. if you're porting a program, also consider writing it in c++ since the program will often be closer to the java variant.
for objc, you'd probably use CF/NS-MutableData
for c++, you can use std::vector
good luck
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.