我无法在nsdata的左侧添加3位(在开始时)。这是我试图转换为iOS的java代码
public byte[] getMessageInFormat(final int MessageType, final int MessageNum, final int MessageNum2, byte[] rawMessage){
byte[][] result = null;
byte[] byteStream = new byte[rawMessage.length + 3];
byteStream[0] = (byte) MessageType;
byteStream[1] = (byte) MessageNum;
byteStream[2] = (byte) MessageNum2;
for (int i=0; i<rawMessage.length; i++)
{
byteStream[3+i] = rawMessage[i];
}
return byteStream;
}
这不起作用
NSData *rawMessage = [@"hello world" dataUsingEncoding:NSASCIIStringEncoding];
NSMutableData * byteStream = [[NSMutableData alloc] init];
[byteStream appendBytes:&MessageType length:sizeof(MessageType)];
[byteStream appendBytes:&MessageNum length:sizeof(MessageNum)];
[byteStream appendBytes:&MessageNum2 length:sizeof(MessageNum2)];
[byteStream appendData:rawMessage];
答案 0 :(得分:0)
[byteStream appendBytes:&MessageType length:sizeof(MessageType)];
没有用,因为MessageType是一个int并将“size of”替换为1个
[byteStream appendBytes:&MessageType length:1];