目前正在做關(guān)于UInt8數(shù)組的相關(guān)應(yīng)用,遇到問(wèn)題。我的代碼:@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;
}下面是我準(zhǔn)備調(diào)用方法充填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中調(diào)用方法的代碼:byte[] myBuffer = new byte[10000];
int n = read(myBuffer);
1 回答

千巷貓影
TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
在Objective-C中可以使用NSDate對(duì)象當(dāng)做byte buffer, 然后dataUsingEncoding獲取字節(jié)表示的字符串:
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
添加回答
舉報(bào)
0/150
提交
取消