If you byval your input strings then a switch from original to bin back to original then = original.
Albert, not as fast as Richard's, but I have a new method.
dec to hex then hex to bin.
The dec to hex I have to speed up.
The hex to bin is very fast.
I have yet to make hex to dec.
Code: Select all
Function DecToHex(DecimalNumber As String) As String
Dim As String starter=DecimalNumber,ans,m,b=String(Len(DecimalNumber),0)
Dim As Ubyte main,carry,pt,c
Dim As Long lens
#macro reverse(s)
lens=Len(s)
For n As Integer=0 To Int((lens-1)/2):Swap s[n],s[lens-1-n]:Next
#endmacro
#macro d16(s,m,c) ''divide by 16
carry=0:ans=s
For z As Long = 0 To Len(s)-1
pt=carry+s[z]-48
main=(pt) Shr 4
carry=( (pt ) And 15)*10
ans[z]=main+48
Next z
c=carry\10
m=Trim(Ltrim(ans,"0"))
#endmacro
Dim As Long idx
Do
c=0
d16(starter,m,c)
Select Case As Const c
Case 10:c=Asc("A")
Case 11:c=Asc("B")
Case 12:c=Asc("C")
Case 13:c=Asc("D")
Case 14:c=Asc("E")
Case 15:c=Asc("F")
Case Else:c+=48
End Select
b[idx]=c
starter=m
idx+=1
Loop Until m="" Orelse m="1"
b=Rtrim(b)
reverse(b)
b=Str(m)+b
Return b
End Function
Function hextobin(Byval hx As String) As String'hex string
Dim As String acc
For n As Long=0 To Len(hx)-1
Var g=Chr(hx[n])
acc+=Bin(Vallng("&h"+g),4)
Next n
Return Ltrim(acc,"0")
End Function
Function bintohex(Byval b As String) As String'bin string
Dim As String hx
Var z=4-Len(b) Mod 4
b=String(z,"0")+b 'pad to *4
For n As Long=1 To Len(b) Step 4
Var g=Mid(b,n,4)
hx+=Hex(Vallng("&b"+g))
Next
Return Ltrim(hx,"0")
End Function
Print "Short number test"
Dim As String g="12345678987654"
Dim As String hx= DecToHex(g)
Print hx
Print Hex(Vallng(g))
Print hextobin(hx)
Print Bin(Vallng(g))
Print
'===================
Print "Big number test"
g=String(1000,"9")
hx=DecToHex(g) 'step 1 get hex
Print hx
Print
Dim As String b=hextobin(hx) 'step 2 get bin from hex
Print b
Print
print "return hex"
Print bintohex(b)
sleep