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

tobias tobiasboe1 at ...20...
Wed Sep 15 15:24:39 CEST 2010


hi,

> hi,
> 
>> I can say, somebyte=ASC("x"), but is it possible to quickly assign a two-byte
>> string to a typical two-byte short?
>>
>> I'm sure this can be done with some lengthy function but (as most
>> programmers should) I have a need for efficiency. I'm not concerned with
>> character set mishaps for people with different language settings, as the
>> app will be virtualized in a strict environment.
>>
>> Also in general I am extremely interested in how to quickly assign values
>> from one datatype to a different one. Does GAMBAS have a convention for this
>> outside of functions like ASC?
>>
>> -----
>> 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
> 
> hum, this shouldn't be so diffcult and even if there were a function, it 
> won't be more efficient than this one:
> 
> PUBLIC FUNCTION GetShort(sWord AS String) AS Short
> 
> 	RETURN Mid$(sWord, 1, 1) * 256 + Mid$(sWord, 2, 1)
> 
> END
> 
> i haven't tried it but i think it should work... but i think there is 
> the problem that shorts in gambas are -32.768 <= Short <= +32.767 and i 
> don't know if there's a possibility to make them unsigned like in c, but 
> if you only want to deal with standard ascii chars (to 127) the highest 
> value will be 127 * 256 + 127 = 32639 and this fits in the short's space.
> i hope, this is all right.
> 
> regards,
> tobi
> 
> ------------------------------------------------------------------------------
> Start uncovering the many advantages of virtual appliances
> and start using them to simplify application deployment and
> accelerate your shift to cloud computing.
> http://p.sf.net/sfu/novell-sfdev2dev
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user

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)


 >> Also in general I am extremely interested in how to quickly assign 
values
 >> from one datatype to a different one. Does GAMBAS have a convention 
for this
 >> outside of functions like ASC?

strings that contain any format of numbers are converted to numbers of 
the adequate datatype by default when needed e.g. in function parameter 
passing and vice versa.
in the gambas doc (language index->conversion functions) you'll find 
that there are some common functions for everything

regards,
tobi




More information about the User mailing list