繁体   English   中英

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

[英]What is the best way to convert this java code into Objective C code?

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

但是不确定如何回读...如果只向缓冲区中添加一个数据,但总共添加了三个数据,这样会更容易,所以我不知道如何回读这些数据。 。

LCYSoft注意:我正在将其作为社区Wiki。 请更正任何问题。 我没有编译这个。 因为您发布了一个方向并且真的想要一个答案,所以我提供了一个方向。 抱歉,我有点忙。

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

我不会为您重写程序,但我将提供一个提示:

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

现在,许多Java程序与c ++程序更加相似。 如果要移植程序,还应考虑用c ++编写它,因为该程序通常更接近于Java变体。

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

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

祝好运

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM