[Gambas-user] An utility component for Gambas
Lewis Balentine
lewis at ...3412...
Mon Dec 1 21:26:22 CET 2014
>> Octal is normally not needed with Gambas. Do you have any use of
that? <<
Neither is "binary" best I can tell. It is still convenient to have
these tools available. One may not always be dealing with numbers
originating in Gambas or output targeting Gambas.
Hex$(), Dec$(), Oct$(), Bin$()
Some might consider the Dec$() function redundant but if the functions
were written such that they would except any common string notation or
value the Dec$() could be used to convert from Hexadecimal or Binary
Strings as well as a integer or long value. The most challenging aspect
might be determining all the various common notations used. I tried
something similar recently for Hexadecimal:
' --------------------------------------
Private Function HexStrToVal(HexStr As String) As Long
Dim i, v As Long
Dim HexCharacters As String = "0123456789abcdef"
Dim N As Long
Dim C As String
' first we remove any white space and convert it to lower case
HexStr = Trim(LCase(HexStr))
' There are a number of other conventions. These are just a few....
If Left(HexStr, 4) = "hex:" Then HexStr = Mid(HexStr, 5)
If Left(HexStr, 3) = "hex" Then HexStr = Mid(HexStr, 4)
If Left(HexStr, 3) = "&#x" Then HexStr = Mid(HexStr, 4)
If Left(HexStr, 2) = "%x" Then HexStr = Mid(HexStr, 3)
If Left(HexStr, 2) = "0x" Then HexStr = Mid(HexStr, 3)
If Left(HexStr, 2) = "0h" Then HexStr = Mid(HexStr, 3)
If Left(HexStr, 2) = "&h" Then HexStr = Mid(HexStr, 3)
If Left(HexStr, 2) = "U+" Then HexStr = Mid(HexStr, 3)
If Left(HexStr, 1) = "x" Then HexStr = Mid(HexStr, 2)
If Left(HexStr, 1) = "h" Then HexStr = Mid(HexStr, 2)
If Left(HexStr, 1) = "$" Then HexStr = Mid(HexStr, 2)
If Left(HexStr, 1) = "#" Then HexStr = Mid(HexStr, 2)
If Left(HexStr, 1) = ":" Then HexStr = Mid(HexStr, 2)
' now just in case we have a space between the prefix and the hex number
' then we add a space to the end so that mid always has something to
return
HexStr = Trim(HexStr) & " "
N = 0
While HexStr <> ""
C = Left(HexStr, 1)
v = InStr(HexCharacters, C)
If v = 0 Then
' like the val function we stop at the first non-hex character
HexStr = ""
Else
v = v - 1
N = (N * 16) + v
HexStr = Mid(HexStr, 2)
Endif
Wend
Return N
End
' --------------------------------------
Someone can probably come up with a more efficient way to calculate the
value.
Regards,
Lewis Balentine
On 12/01/2014 01:35 PM, Benoît Minisini wrote:
> Le 01/12/2014 19:11, T Lee Davidson a écrit :
>> On 12/01/2014 08:48 AM, Benoît Minisini wrote:
>>> Hi,
>>>
>>> I'd like to create a 'gb.util' component, written in Gambas, to collect
>>> useful non-GUI programming routines that can't go directly into the
>>> interpreter because it would make it too heavy.
>>>
>>> [snip]
>>>
>>> The difficulty will be to find the best interface (class name, method
>>> name, where to put it) for that.
>>>
>>> For example, for the removing accents method, I extended the String
>>> class. Logical... For directory methods, I created a new Shell static
>>> class...
>>>
>>> [snip]
>> I'm not sure if you are asking for thoughts on miscellaneous functions
>> to put in a gb.Util component or thoughts on useful extensions to
>> existing classes, or both. But...
>>
>> A. Format decimal numbers as octal:
>> Public Sub Format(iDecimal as Integer) as String
>> Dim digit as Byte
>> Dim out as String
>> While (iDecimal > 0)
>> digit = iDecimal % 8
>> iDecimal /= 8
>> out = digit & out
>> Wend
>> Return out
>> End
> Maybe an Oct$() function inside the interpreter. Octal is normally not
> needed with Gambas. Do you have any use of that?
>
>> B. It would be nice to be able to work with *nix-native timestamps.
>> Perhaps this might be relatively easily accomplished by just a couple
>> functions:
>> 1. DateToTimestamp(dDate as Date) as Integer
>> 2. TimestampToDate(iTimestamp as Integer) as Date
>> Format$ should be able to take over from there.
>>
> Noted!
>
More information about the User
mailing list