[Gambas-devel] ideas for two new string functions
Kevin Fishburne
kevinfishburne at ...590...
Wed Nov 10 03:07:39 CET 2010
I'm currently having a hell of a time finding any way, much less an
efficient one, to send multiple datatypes through a UDP socket as a
single packet, then receive and re-assign them on the other end.
The solution I've come with with is to create two conversion functions
for each datatype. The first function converts the passed variable to a
binary string and the second interprets the corresponding binary string,
converting it back to its native datatype. Here's the obvious example:
PUBLIC FUNCTION Byte2Binary(source AS Byte) AS String
' Convert a byte datatype to a binary string and return it.
RETURN Chr$(source)
END
PUBLIC FUNCTION BinaryToByte(source AS String) AS Byte
' Convert a binary string to a byte datatype and return it.
RETURN Asc(source)
END
Since we have Chr and Asc this is super easy, in fact, these my
functions aren't really necessary in this case because it's so simple.
What happens when working with single's, long's, float's, etc. however?
Chr and Asc only work with single characters/bytes and aren't useful for
2, 4 or 8-byte datatypes.
The hack I'm about to implement is to write the (let's say short)
datatype to a temporary file, immediately read it back into a string and
return the string. That gives me a nice "Short2Binary" function so I can
then add the two-byte string to the packet being sent through the UDP
socket. The corresponding conversion function, "Binary2Short" uses
something like this:
PUBLIC FUNCTION Binary2Short(source AS String) AS Short
' Convert a binary string to a short datatype and return it.
RETURN Asc(Mid$(source, 2, 1)) * 256 + Asc(Mid$(source, 1, 1))
END
While that's so hacked-up I might be mistaken for an axe murderer, it
should work.
My suggestion, if it sounds useful to anyone, would be to make two new
string functions similar to Chr and Asc but that work with more than one
byte. One could then easily convert variables to strings, which could be
sent as part of a network packet, then convert them back to their
original datatypes on the receiving end.
Please let me know if this is completely stupid because (hopefully)
there is some other way. Thanks all.
--
Kevin Fishburne
Eight Virtues
www: http://sales.eightvirtues.com
e-mail: sales at ...590...
phone: (770) 853-6271
More information about the Devel
mailing list