Java 传递(指向UInt8数组的指针)并访问Objective-C中的数组
我需要使用UInt8数组,但我不知道如何正确地执行此操作 这是我的代码:Java 传递(指向UInt8数组的指针)并访问Objective-C中的数组,java,iphone,objective-c,xcode,Java,Iphone,Objective C,Xcode,我需要使用UInt8数组,但我不知道如何正确地执行此操作 这是我的代码: @interface MyClass : NSObject { __strong id * myArray; //private byte[] myArray; <- Java code } @property (nonatomic,readwrite) __strong id * myArray; @end 在Objective-C中,您可以使用NSData对象作为字节缓冲区,并使用datausin
@interface MyClass : NSObject {
__strong id * myArray; //private byte[] myArray; <- Java code
}
@property (nonatomic,readwrite) __strong id * myArray;
@end
在Objective-C中,您可以使用
NSData
对象作为字节缓冲区,并使用datausingencode
获取字符串的字节表示:
NSString *aString = @"theString";
NSData *myBuffer = [aString dataUsingEncoding:NSUTF8StringEncoding];
const char *bytes = [myBuffer bytes]; // pointer to the bytes in the buffer
NSUInteger count = [myBuffer length]; // number of bytes in the buffer
如果
bufferTmp[]
与函数的“结果”没有关系,你为什么要麻烦地使用它呢?你是说在Java中-对不起,这是一个输入错误,我更正了。基本上,该方法只是用字符串的字符填充传入的字节数组…我不理解您的“等效工作Java代码”:未使用msg
参数,它返回一个值,但返回类型是void
…是的,它将缓冲区的长度返回为int,我在上面的代码中更正了这一点。。。
UInt8 myBuffer[10000];
[xxx read: myBuffer]; <- how to do this correctly ?????
public int getArray(byte[] bufferTmp) {
String theString = "theString";
for (int i = 0; i < bytes; i++) {
char c = theString.charAt(i);
bufferTmp[i] = (byte) c;
}
return bytes;
}
byte[] myBuffer = new byte[10000];
int n = read(myBuffer);
NSString *aString = @"theString";
NSData *myBuffer = [aString dataUsingEncoding:NSUTF8StringEncoding];
const char *bytes = [myBuffer bytes]; // pointer to the bytes in the buffer
NSUInteger count = [myBuffer length]; // number of bytes in the buffer