[Gambas-user] someshort="xy" returns "Wanted short, got string instead"

kevinfishburne kevinfishburne at ...1887...
Sun Oct 10 01:37:12 CEST 2010



Benoît Minisini wrote:
> 
>> > I can only think of two solutions here. One is to find a way to convert
>> a
>> > two-character string to a short as quickly as possible (the subject of
>> > the post), and the other is to change the way I'm collecting the data
>> to
> 
> I have neve seen that mail, Kevin. Did it go to the mailing-list?
> 
> If I resume, you want to send the contents of an array of short on a UDP 
> socket, and get it back on the other end of the socket?
> 

I generally post through the Nabble forum, as direct mailing list emails
have been placed in a queue that never gets processed. I'll leave that to a
later date to solve.

Basically I'm sending mixed datatypes as a single UDP packet. There may be
1024 Bytes followed by 512 Shorts, etc., in a known sequence that can be
interpreted by the client.

When the client interprets the data in the received UDP packet it needs to
assign different pieces of it to variables of varying datatypes. The string
is constructed on one side then deconstructed on the other. There may be
something fundamental that I'm missing (no surprise), but the packet data is
being received as a String and as such I must convert segments of that
string to the corresponding datatypes.

I'm currently trying to write a function to convert a four-byte segment of
the string to a Single, and have a conversion module for all the other
datatypes that could be encoded in the UDP string.

My current conversion function for Shorts is similar to Tobi's suggestion:

PUBLIC FUNCTION ToShort(source AS String) AS Short

  ' Convert a two-byte string to a short datatype value.

  RETURN Asc(Mid$(source, 2, 1)) * 256 + Asc(Mid$(source, 1, 1))

END

This works well, but I need similar functions for most common datatypes
(Integer, Long, Single, Float). Is this possible, or am I making a
fundamental programming mistake and there is an easier way? The app in
question will push the limits of typical upstream bandwidth so I need to
keep the packet size/frequency as low as possible. Most network transactions
will not consist of arrays, but a few values that must be sent as raw data
rather than numerical text strings.

-----
Kevin Fishburne, Eight Virtues
www:  http://sales.eightvirtues.com http://sales.eightvirtues.com 
e-mail:  mailto:sales at ...1887... sales at ...1887... 
phone: (770) 853-6271
-- 
View this message in context: http://old.nabble.com/someshort%3D%22xy%22-returns-%22Wanted-short%2C-got-string-instead%22-tp29715645p29925084.html
Sent from the gambas-user mailing list archive at Nabble.com.





More information about the User mailing list