Convert NSData to a simple Char

Discussion in 'Mac Programming' started by tanakasan1734, Jan 14, 2010.

  1. tanakasan1734 macrumors newbie

    Joined:
    Jan 14, 2010
    #1
    Hi,

    I am up to chapter 17 in the Steve Kochan Obj-C book and have used c for a year or so, basically I'm pretty new to this. I am trying to write a simple app to read simple chars sent wirelessly to a serial receiver.

    I have used the AMSerial class (from harmless cocoa) to read data from a serial input port (keyspan serial to usb) and I've managed to get the data I'm transmitting to this port to be printed on stdout with an NSFileHandle. The issue is that I need to take the 256bit packet (an unsigned char) and convert it to an int and then perform some arithmetic on it.

    The problem comes when I use the NSData "bytes" method to put the value into a char.

    Can anyone help me to get the char value out of NSData and into a simple 8bit byte so I can use it as an int please?

    The problematic code section is posted below. Hopefully there's enough for someone to help. The current code prints the data correctly on the the console but when I try to use it as an int I think I'm only getting the memory address, not too sure though.

    Thanks in advance to anyone who can help, and thanks also to Andreas of harmless cocoa for keeping serial alive and easy (ish) to use on a mac

    Cheers

    Simon

    Code:
    NSData              *rxData = [NSData data];  
    NSFileHandle      *sh;  
    unsigned char     data, *d;      //unsigned to get full 256 range  
    float                  value;   
    
    .
    .
    .
    .
    .
    .
    
    rxData = [sh readDataOfLength: sizeof(char)];  
                            d = [rxData bytes];  
                            data = *d;  
                            value = data;  
                            value = (256/value) * 0.9;
    
    
     
  2. tanakasan1734 thread starter macrumors newbie

    Joined:
    Jan 14, 2010
    #2
    Problem solved

    Problem solved, I'm a muppet.

    Simon
     

Share This Page