Re: Performer and networking

New Message Reply Date view Thread view Subject view Author view

From: Teemu.Makipatola++at++vtt.fi
Date: 11/16/2001 01:51:52


>This is a little off subject but I am integrating some networking into my
>Performer app so that I can update my position from an external source and
>when receiving a 2 byte number that I know should be 2, I receive 512. This
>is obviously a byte ordering problem so I have used ntohs to try and swap
>the ordering of the bytes but I still get 512 after using ntohs.
>
>Does anyone know how I can successfully swap the order of the bytes ??

This might not be the most sophisticated way but if everything else fails you
could make the numbers into same byte size char arrays and transmit them as
char arrays. Then you could make a test number that would be transmitted
first and the receiver would store in a flag if the bytes need to be
swapped or not.

Float to chars would be translated something like this:

  int nbytes=sizeof(float);
  char buffer[nbytes];
  memcpy(&buffer[0], (char*) &floatvalue, nbytes);

The float value we want to convert into chars would be in a float variable
floatvalue. Now buffer would contain the same bytes than the float.

The conversion back to float in the other end:
 if (swapbytes) swapByteOrder(buffer);
 memcpy((char*) &floatvalue, &buffer[0], nbytes);

If the test numbers do not match the bytes in buffer could be reversed
(swapbytes would be TRUE). The function swapByteOrder would be made by you
just to reverse the byte ordering of the given char array.

I haven't tried this so the code might have errors but in general it's one
way to do it.

Teemu Mäki-Patola / VTT, Finland


New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Fri Nov 16 2001 - 01:51:35 PST

This message has been cleansed for anti-spam protection. Replace '++at++' in any mail addresses with the '@' symbol.