One issue is that you are reading 8 bytes at the time from the string by using a UlongInt pointer. That produces 8 ascii digits, which then gets converted to binary, so it will fit easily in a 32 bit Ulong.
It is important that the string be a multiple of 8 digits long, or there will be a bounds error when you read the end of the string.
It is a pity we cannot compute y Div 10, and keep the y Mod 10 remainder for free, as happens in assembly code. Maybe if 10 was declared as a constant, a deep optimiser could recognise the case.
Here is an example with loops converted to straight code.
Code: Select all
'---------------------------------------------------------
' Conversions using Ulongint, pointer indexed from a string
' Start with 8 ascii digits in x, a UlongInt = 64 bit integer
' convert those 8 digits to y, a ULong = 32 bit integer
' then convert a Ulong y, back to z, as 8 ascii digits in Ulongint
'---------------------------------------------------------
' make state variables
Dim As Ulongint x, z
Dim As Ulong y
'---------------------------------------------------------
' make input and output uLL pointer indexed strings
Dim As String num8 = "00000000", text = num8
Dim As Ulongint Ptr p_num8 = Cast( Ulongint Ptr, Strptr( num8 ) ) ' input
Dim As Ulongint Ptr p_text = Cast( Ulongint Ptr, Strptr( text ) ) ' output
'---------------------------------------------------------
Dim As Ulongint ascii_base = *p_num8
Print ascii_base
Print 3472328296227680304uLL ' Albert's magic number
Print
Randomize
'---------------------------------------------------------
' make a random string of 8 digits
For i As Integer = 0 To 7 ' direction irrelevant
num8[ i ] = Asc( "0" ) + Int( Rnd * 10 )
Next
x = *p_num8
Print " input "; num8
'---------------------------------------------------------
' convert 8 ascii bytes in Ulongint x to uLong y
x = *p_num8 - ascii_base ' refer to ascii zero
'y = 0
'For i As Integer = 0 To 56 Step 8 ' reverse loop reverses endian
' y = ( ( x Shr i ) And &h0F ) + ( y * 10 )
'Next i
' expand above 4 lines
y = ( ( x Shr 0 ) And &h0F )
y = ( ( x Shr 8 ) And &h0F ) + y * 10
y = ( ( x Shr 16 ) And &h0F ) + y * 10
y = ( ( x Shr 24 ) And &h0F ) + y * 10
y = ( ( x Shr 32 ) And &h0F ) + y * 10
y = ( ( x Shr 40 ) And &h0F ) + y * 10
y = ( ( x Shr 48 ) And &h0F ) + y * 10
y = ( ( x Shr 56 ) And &h0F ) + y * 10
Print Using " uLong ########"; y
'---------------------------------------------------------
' convert a uLong y, back into Ulongint z, then to ascii string
'For i As Integer = 0 To 7 ' reverse loop to reverse endian
' z = ( z Shl 8 ) + ( y Mod 10 ) : y = y \ 10
'Next i
' expand above 3 lines
z = ( y Mod 10 ) : y \= 10
z = ( z Shl 8 ) + ( y Mod 10 ) : y \= 10
z = ( z Shl 8 ) + ( y Mod 10 ) : y \= 10
z = ( z Shl 8 ) + ( y Mod 10 ) : y \= 10
z = ( z Shl 8 ) + ( y Mod 10 ) : y \= 10
z = ( z Shl 8 ) + ( y Mod 10 ) : y \= 10
z = ( z Shl 8 ) + ( y Mod 10 ) : y \= 10
z = ( z Shl 8 ) + ( y Mod 10 )
*p_text = z + ascii_base
Print "output "; text
'---------------------------------------------------------
If num8 <> text Then
Print " Error detected."
Sleep
Else
Print " Correct."
End If
Print
Sleep
'---------------------------------------------------------