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

kevinfishburne kevinfishburne at ...1887...
Wed Sep 15 20:43:45 CEST 2010



tobias-47 wrote:
> 
> i forgot the asc() functions, of course:
> 
> PUBLIC FUNCTION GetShort(sWord AS String) AS Short
> 
> 	RETURN Asc(Mid$(sWord, 1, 1)) * 256 + Asc(Mid$(sWord, 2, 1))
> 
> END
> 
> or alternatively, you can assign the positions in the asc() function, like
> 
> RETURN Asc(sWord, 1) * 256 + Asc(sWord, 2)
> 

Thanks Tobi, I'll give that a try if it becomes the only option. Hopefully
there's something I can do like a variable pointer, so the data can
basically just be copied from the string directly. This is for a real-time
game so any inefficiencies can cause it to "hang" for a second and interrupt
the smoothness of play.


Benoît Minisini wrote:
> 
>> I can say, somebyte=ASC("x"), but is it possible to quickly assign a
>> two-byte string to a typical two-byte short?
> 
> It's a bad idea. What do you want to do exactly?
> 

Hi Benoît. Maybe there's a better way for me to have come to this point, but
here's what's happening:

A server app retrieves sets of data from five binary files and stores them
consecutively in a string. The first set is a 48x48 grid of shorts for a
total of 4608 bytes (48x48x1x2). The last four sets are 48x48 grids of bytes
for a total of 9216 bytes (48x48x4x1). So in total we have 13824 bytes (2304
shorts and 9216 bytes), converted to one large string something like this:

DIM data AS String
DIM chunk AS String

READ #somefileofshorts, chunk, 2
data = data & chunk

READ #somefileofbytes, chunk, 1
data = data & chunk

Normally I might read the shorts into an array of shorts, but in this case
the final string is sent to a client app as a UDP packet. I also need to
send all the data as a single packet rather than several. The client app
receives the packet and parses it, assigning the floats and bytes of the
payload to five 48x48 element arrays like this:

DIM somearray AS Short[48, 48]

somearray[x, y] =  Mid$(payload, position, 2)

It needs to make that string to short conversion 2304 times (48x48) in as
little time as possible.

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 be sent
as a UDP packet so that it can be read more "normally" by the client. The
UDP_Read procedure is generic however, and has no way of knowing what kind
of data is contained within the packet, so that may be difficult at best.

-----
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-tp29715645p29721157.html
Sent from the gambas-user mailing list archive at Nabble.com.





More information about the User mailing list