Forums > Dev

[OT] Formatting int32 in OSC UDP packets

August 31, 2008 | 11:26 pm

I downloaded the iPhone SDK over the weekend to mess around with making an OSC based controller to test on the simulator. While it took a bit of puzzling at first (network programming is new to me) I got my BSD socket code working and was able to receive the OSC packets in Max with [udpreceive]. I can send strings fine, but I’m running into problems sending an integer (haven’t bothered to try floats). This is the first time I’ve really needed to pay attention to individual bytes before and I’m guessing it is just a problem with how I’m breaking up the INT. Max receives the messages, but I am ending up with big negative numbers instead 394. At first I thought it was a signing problem or maybe I was using the wrong bitwise operator so I think I ran through all the possibilities of signed/unsigned ints and chars in this example as well as switching to < < instead of >>.

initSocket() and sendPacket(char[], int) are my functions for creating the UDP socket and sending the buffer, both of these work fine when I’m sending an OSC string. Can anyone see what I’m doing wrong with formatting the int? Thanks in advance for the help!

/*my function for opening a socket */
initSocket();
int testint = 394;
char a[12] = { ‘/’, ‘h’, ‘i’, ", ‘,’ , ‘i’, ", " };
for (int j = 8; j < 12; j++)
{
a[j] = (char)testint >> (j * 8);
}
/*custom function to send BSD packet
*int sendPacket(char sendBuffer[], int buffer_length)
*/
sendPacket(a,12);


September 1, 2008 | 8:52 am

On 1 Sep 2008, at 00:26, Roth Michaels wrote:

> int testint = 394;
> char a[12] = { ‘/’, ‘h’, ‘i’, ", ‘,’ , ‘i’, ", " };
> for (int j = 8; j < 12; j++)
> {
> a[j] = (char)testint >> (j * 8);
> }

"(j – 8) * 8" surely? (Or maybe "(11 – j) * 8" – I’m a little rusty on
OSC endedness.) Oh, and I can’t remember whether cast binds more
tightly than shift – it probably does, in which case you won’t get the
higher bytes. (But my C is even more rusty than my OSC.)

– N.

Nick Rothwell / Cassiel.com Limited
http://www.cassiel.com
http://www.myspace.com/cassieldotcom
http://www.last.fm/music/cassiel
http://www.reverbnation.com/cassiel
http://www.linkedin.com/in/cassiel
http://www.loadbang.net


September 1, 2008 | 9:20 am

>
> "(j – 8) * 8"

"(j – 8) * 8" indeed…I guess I forgot to fix that when I changed the start point of my for loop. That didn’t completely solve my problem in that now I am getting a different negative number, but there is one mistake out of the way. Now that you’ve pointed that out, I’ll look at this some more once I get some sleep.


September 1, 2008 | 6:03 pm

Intel processor presumably. Intel is wrong-endian; network is right-endian. I’m pretty sure OSC uses network endianess.

I would be inclined to try something like the following to be shielded from processor-specific issues.

—-

#include // Or use Apple’s byte-swapping .h file

long temptemp = BYTEORDER_MSBW32(tempint);
char* bytevec = (char*) &temptemp;

for (j = 0; j < 4; j += 1) a[j+8] = bytevec[j];

—-

Untested code. But I think it handles the byte-order issues correctly.

Good luck — P.


September 2, 2008 | 12:57 am

Quote: Peter Castine wrote on Mon, 01 September 2008 14:03
—————————————————-
> Intel processor presumably. Intel is wrong-endian; network is right-endian. I’m pretty sure OSC uses network endianess.
>
> I would be inclined to try something like the following to be shielded from processor-specific issues.
>
> —-
>
> #include // Or use Apple’s byte-swapping .h file
>
> long temptemp = BYTEORDER_MSBW32(tempint);
> char* bytevec = (char*) &temptemp;
>
> for (j = 0; j < 4; j += 1) a[j+8] = bytevec[j];
>
> —-
>
> Untested code. But I think it handles the byte-order issues correctly.
>
> Good luck — P.
>
>
—————————————————-

Yup, Peter it was a byte order problem. I used htonl() to fix the problem. This was in the code when I was first having problems but I took it out thinking perhaps my problem was needlessly changing byte order when as Nick pointed out it was just sloppy changes I made to my for() loop.

I guess I assumed my problems were because I always semi-ignored the byte level stuff in my programming books when it was just sloppy coding. I guess that’s what I get for coding side projects when I should be sleeping. Thanks to both of you for the help.


Viewing 5 posts - 1 through 5 (of 5 total)