Assigning Binary Data From Network To Variables

  • Hello,
      I am very new to Cocoa, so I apologize if this question seems very basic.
    Right now I am trying to read data from a program that is sending data in
    the "TUIO" protocol.  I have read the data from the socket, and assigned it
    to a NSData object with this code:
        NSData *messageData = [[notification userInfo]
                            objectForKey:NSFileHandleNotificationDataItem];
    Now how do I translate that data to integer and float values?  I have sample
    code from C++ and Java, but I don't know how to translate them to Cocoa.  In
    Java, it looks like this:
            Object[] args = message.getArguments();
            String command = (String)args[0];
            String address = message.getAddress();

            int s_id  = ((Integer)args[1]).intValue();
            int f_id  = ((Integer)args[2]).intValue();
            float x = ((Float)args[3]).floatValue();
            float y = ((Float)args[4]).floatValue();

    In C++ it looks like this:
            ReceivedMessageArgumentStream args = msg.ArgumentStream();
            ReceivedMessage::const_iterator arg = msg.ArgumentsBegin();
                    int32 s_id, f_id;
                    float xpos, ypos, angle, xspeed, yspeed, rspeed, maccel,
    raccel;

                    args >> s_id >> f_id >> xpos >> ypos >> angle >> xspeed >>
    yspeed >> rspeed >> maccel >> raccel >> EndMessage;

    I would also be happy on a hint on what framework to use to do something
    like this, but right now I am not even sure where to start looking.

      Thank You,
        Bridger Maxwell
  • On Jan 5, 2008, at 12:27 AM, Bridger Maxwell wrote:

    > I am very new to Cocoa, so I apologize if this question seems very
    > basic.
    > Right now I am trying to read data from a program that is sending
    > data in
    > the "TUIO" protocol.  I have read the data from the socket, and
    > assigned it
    > to a NSData object with this code:
    > NSData *messageData = [[notification userInfo]
    > objectForKey:NSFileHandleNotificationDataItem];
    > Now how do I translate that data to integer and float values?

    Try something like this (warning, written in Mail, untested, use at
    your own risk, but this ought to work):

    long long offset = 0;
    int32_t someIntValue;
    float32_t someFloatValue;

    [messageData getBytes:&someIntValue range:NSMakeRange(offset,
    sizeof(int32_t))];
    offset += sizeof(int32_t);
    [messageData getBytes:&someFloatValue range:NSMakeRange(offset,
    sizeof(float32_t))];
    offset += sizeof(float32_t);

    Don't forget to flip those bits around on the appropriate
    architectures if you're building a universal binary. There are some
    NSSwap... functions in Foundation that will do that.

    Nick Zitzmann
    <http://www.chronosnet.com/>
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today