send a float number using a udp connection

i’m stuck here.
i want to send a float number via udp connection. but the class says it needs char data.

how do i change this float to string and then to char data?
i’ve tried ofToString and as the name says it converts to string. :slight_smile:


ps: im using ofxUDPManager

(moved to extend since it relates to addons… - zach )

does this not work?

ofxOscMessage m;  
m.setAddress( "/test" );  
m.addFloatArg( ofGetElapsedTimef() );  
sender.sendMessage( m );  

that’s (trimmed down) from the example code - it sends a float when you hit the letter “a” – just the elapsed time in seconds. if you run the serd/recv examples you can see it in action by hitting “a” on the send example… take a look in keypressed() for how it’s done.

take care!!

If you want to use the ofxUDPManager you can do something like.

char tmp[32];  
memset(tmp, 0, 32);  
sprintf(tmp, "%f", yourFloatValue);  
udp.send( tmp, strlen(tmp) );   

I think that should work.


director receives OSC data as #bundle, and i don’t know how to encode that, and i don’t want to waste my cpu cycles in director encoding OSC packet data.
instead i’ve used:

in testApp.h

ofxUDPManager udpmanager;  

in testApp.cpp::setup()


in testApp.cpp::draw()

string newstring = ofToString(overall.x);  
char *buf = new char[128];  
for(size_t i = 0; i < newstring.size(); i++)  
   buf[i] = newstring[i];  
//...When you are done with buf...  
delete []buf;  

and yes, theo’s code also works. I’m not a hardcore c++ programmer, so i don’t know which one executes faster.