1

こんにちは、私は 1 秒あたり 512 の値を送信する外部デバイスからのデータをキャプチャする必要がある iPAD 用のアプリケーションを作成しています。私は同じためにAppleのExternal Accessories APIを使用し、以下のようなコードを持っています:-

- (void)stream:(NSStream*)theStream handleEvent:(NSStreamEvent)streamEvent  
{  
    switch (streamEvent)  
 {  
   case NSStreamEventNone:  //Sent when open complete
  //NSLog(@"NSStreamEventNone");
   break;   
  case NSStreamEventOpenCompleted:  //Sent when open complete

  //NSLog(@"NStreamEventOpenCompleted: %@", theStream);
  break ;

  case NSStreamEventHasBytesAvailable:
    {
   //NSLog(@"NSStreamEventHasBytesAvailable");

    uint8_t buffer[1024];
    unsigned int len=0;

    len=[(NSInputStream *)theStream  read:buffer maxLength:1024];
    if(len>0){      

    NSData* data=[NSData dataWithBytes:buffer length:len];

    NSString *value = [[NSString alloc] initWithData:dataencoding:NSASCIIStringEncoding];

   }
    }
 break;   

 case NSStreamEventHasSpaceAvailable:  
    os = theStream;
    break;
 case NSStreamEventErrorOccurred:  
   //NSLog(@"NSStreamEventErrorOccurred");
    break;   

  case NSStreamEventEndEncountered:  
    ////NSLog(@"NSStreamEventEndEncountered");
    break;   
  default:  
    break;  
  }  
 }

これから得られるのは、適切なエンコードのない文字化けしたテキストです。私も以下を使ってみました:-

 NSString *value = [[NSString alloc] initWithData:dataencoding:NSUTF8StringEncoding];

しかし、これはnullを返し続けます。編集:- NSLog を実行すると、以下が表示されます:-

 vªª€ÿù…ªª€ ÿûƒªª€ÿK3ªª€ÿ£Ûªª€ÿ˳ªª€ÿ}ªª€ÿ噪ª€

ここで整数を取得することになっています。誰かが私がここで間違っていることを教えてください。

4

2 に答える 2

3

NSLog は、拡張 ASCII 文字の文字列のようです。それらがApple拡張Ascii文字セットからのものであると仮定すると、リストしたスニペットの開始によって表されるuint8_t(1バイト)データは次のとおりです。

€ÿù…ªª€...

10 進数で表すと、次のようになります。

172, 196, 26, 248, 26, 192, 172, 214, 172, 34, 172 ...

これはあなたのデータですか?出力をテストするコードを次に示します。

NSString *receivedString = @"€ÿù…ªª€"; // Change this to the string variable you were using with NSLog

for (int i=0; i<[receivedString length]; i++) 
{
    unsigned char asciiCode = [ receivedString characterAtIndex:i];
    NSLog( @"Value at %d is %d", i, asciiCode ); 
}
于 2012-12-03T07:52:53.703 に答える
0

受け取るデータは生の値です。ほとんどの場合、それらは UTF8 文字ではありません。ほとんどの場合、NSData のバイトから直接、各 2 バイトを 2 バイト整数に入れる必要があります。

于 2012-12-09T09:24:11.003 に答える