views:

25

answers:

2

So I'm writing an application for my iphone that networks to my computer running a java application using a socket. But when I'm transferring data, I have to send a NSData object from my iphone, and of course the java program doesn't know how to interpret it. How can I fix this? I need to send 3 float values between the two applications.

Any help would be appreciated...

A: 

You said you have to send a NSData object - why?

If you can, send it as a String and do a Float.parseFloat(str); on the Java end?

If you need to send the three values in one, come up with a delimiter to put between them, such as ;, and do a str.split(";"); in Java. This would give you a String[], and you can parse each value using the method mentioned above. Simplified, something like this:

String delimitedFloats = readFloatsFromSocket();
for (String floatStr : delimitedFloats.split(";"))
{
    float f = Float.parseFloat(floatStr);
    doSomething(f);
}

DISCLAIMER: I don't even know that NSData is, so please excuse me if I'm way off or missing something obvious.

Lauri Lehtinen
+2  A: 
float a,b,c;
NSString *s = [NSString stringWithFormat:@"%g,%g,%g",a,b,c];
NSData *d = [s dataWithEncoding:NSUTF8StringEncoding];
// send d

...

byte[] b = ...; // read data
String s = new String( result );
String[] ss = s.split( "," );
float a = Float.parseFloat( ss[0] );
float b = Float.parseFloat( ss[1] );
float c = Float.parseFloat( ss[2] );
drawnonward
awesome thanks!
Deniz O