0

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];       <-      how to do this correctly ?????

これは同等の動作する 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);
4

1 に答える 1

2

Objective-C では、NSDataオブジェクトをバイト バッファーとして使用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
于 2013-02-20T19:21:44.057 に答える