目前正在做關於UInt8數組的相關應用,遇到問題。
我的代碼:
@interface MyClass : NSObject {
__strong id * myArray; //private byte[] myArray; <- Java code
}
@property (nonatomic,readwrite) __strong id * myArray;
@end
在MyClass中的方法:
-(int) getArray: (__strong id *) bufferTmp {
NSString* aString = @"theString";
int bytes = aString.length;
//now I need to fill the passed in array with the chars of the String
for (int i = 0; i < bytes; i++) {
char c = [aString characterAtIndex:i];
??? bufferTmp[i] = (UInt8)c; <----- what to write here?
}
return bytes;
}
下面是我准備調用方法充填myBuffer的代碼:
UInt8 myBuffer[10000];
[xxx read: myBuffer]; <- 不知道這段正確麼?
這是相同的java代碼:
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;
}
在java中調用方法的代碼:
byte[] myBuffer = new byte[10000];
int n = read(myBuffer);
在Objective-C中可以使用NSDate對象當做byte buffer, 然後dataUsingEncoding獲取字節表示的字符串:
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