将此java代码转换为Objective C代码的最佳方法是什么?

时间:2011-02-14 06:57:08

标签: java objective-c

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();
}

我有两个java方法,想写一个客观的C方法.. 对于第一种方法,我把它写成了像

这样的Objective C代码
- (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;
}

但不确定如何阅读它... 如果我只将一个数据添加到缓冲区中可能会更容易 但我总共添加了三个数据,所以我不知道如何阅读这些数据.. 提前谢谢......

2 个答案:

答案 0 :(得分:2)

LCYSoft注意:我正在将其作为社区维基。请更正任何问题。我没有编译这个。既然你发布了一个方向而真的想要一个答案,我提供了一个。对不起,我有点忙碌。

这展示了两个方向,并扩展了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;
}

答案 1 :(得分:1)

我不打算为你重写程序,但我会提供一个提示:

你可以在objc程序中使用c ++。具体来说,您可以编译为C(.c),ObjC(.m),C ++(。cpp)和ObjC ++(。mm)。注意:每种语言都有一个常见的扩展名。编译器(默认情况下)将使用文件扩展名隐含的语言进行编译。

现在,许多java程序更类似于c ++程序。如果你正在移植一个程序,也可以考虑用c ++编写它,因为程序通常更接近java变种。

对于objc,你可能会使用CF/NS-MutableData

对于c ++,您可以使用std::vector

祝你好运

相关问题