Search code examples
objective-cios5xcode4

how to display ascii bytes in xcode/objective-c?


I'm looking at Apple's EADemo project, showing users how to use the External Accessory Framework. Could someone tell me how to display or "cout" the data received instead of just incrementing the number of bytes received? (I'm brand new to objective-c, ios, and all things apple!)

- (void)_sessionDataReceived:(NSNotification *)notification
{
    EADSessionController *sessionController = (EADSessionController *)[notification object];
    uint32_t bytesAvailable = 0;

    while ((bytesAvailable = [sessionController readBytesAvailable]) > 0) {
        NSData *data = [sessionController readData:bytesAvailable];
        if (data) {
            _totalBytesRead += bytesAvailable;
        }
    }

    [_receivedBytesLabel setText:[NSString stringWithFormat:@"Bytes Received from Session: %d", _totalBytesRead]];
}

And how would I change that last line to display the ascii data received instead of bytes received?

Thanks!


Solution

  • Just make an NSString from the data:

    if (data) {
        _totalBytesRead += bytesAvailable;
        NSString *asciiStringFromData = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        NSLog(@"ASCII bytes read: %@", asciiStringFromData);
    }
    

    If you need a different encoding, here's the list.