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

Kevin Fishburne kevinfishburne at ...1887...
Tue Oct 12 06:33:44 CEST 2010


  I've had some trouble posting to the various GAMBAS mailing lists in 
the past and so have been using the Nabble forum, which generally has 
echoed my posted to the proper mailing list. It seems to not have been 
working so well recently though. As such I'm reposting my last post 
directly to the mailing list and hopefully it will be delivered. Here's 
the original thread for reference:

http://old.nabble.com/someshort%3D%22xy%22-returns-%22Wanted-short,-got-string-instead%22-td29715645.html#a29732625

And here's my post:

    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
e-mail: sales at ...1887...
phone: (770) 853-6271





More information about the User mailing list