Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ZeroBE64$ = Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0)
- OneBE64$ = Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(0) + Chr$(1)
- Function DumpBE64(A$)
- out$ = "0x"
- For I = 1 To 8
- out$ = out$ + Right$(Hex$(Asc(Mid$(A$, I))), 2)
- Next
- Print out$
- End Function
- Print "DumpBE64 Tests:"
- DumpBE64 ZeroBE64$
- DumpBE64 OneBE64$
- ;**
- ;* Converts an integer To String representation
- ;*
- ;* Returns the String representation of num. The conversion is done byte-wise
- ;* with the high-nibble First.
- ;*
- ;* @param num An integer
- ;*
- ;* @Return The String representation of the given integer
- ;*/
- Function EncodeBE64$(num)
- result$ = ""
- For I = 1 To 8
- result$ = Chr$(num And 255) + result$
- num = num Shr 8
- Next
- Return result$
- End Function
- Print "EncodeBE64 Tests:"
- DumpBE64 EncodeBE64$(1)
- DumpBE64 EncodeBE64$(256)
- ;**
- ;* Converts a String representation to an integer
- ;*
- ;* Returns the integer representation of num$. The conversion is done byte-wise
- ;* with the high-nibble First.
- ;*
- ;* @param num$ An integer
- ;*
- ;* @Return The integer equivalent of the given string
- ;*/
- Function DecodeBE64(num$)
- result = 0
- For I = 1 To 8
- A = Asc(Mid$(num$, I))
- If A < 0 Then A = 0
- result = result Shl 8 + A
- Next
- Return result
- End Function
- Print "DecodeBE64 Tests:"
- Print DecodeBE64(ZeroBE64$)
- Print DecodeBE64(OneBE64$)
- Print DecodeBE64(EncodeBE64$(65535))
- ;Stop here if you have 64-bit interpreter
- ;A$ = EncodeBE64$($7fffffffffffffff)
- ;B$ = EncodeBE64$($0123456789ABCDEF)
- ;Q = DecodeBE64(A$) / DecodeBE64(B$)
- ;R = DecodeBE64(A$) Mod DecodeBE64(B$)
- ;Print Q ;Rem Windows Calc says "0x70"
- ;Print R ;Rem Windows Calc says "0x91A2B3C4D5E76F"
- ;C$ = EncodeBE64$(Q)
- ;D$ = EncodeBE64$(R)
- ;WaitKey
- ;End
- ;**
- ;* Compare two numbers
- ;*
- ;* Compares the num1 To the num2 And returns the result as an integer
- ;*
- ;* @param num1$ The Left operand, as a big-endian String
- ;* @param num2$ The Right operand, as a big-endian String
- ;*
- ;* @return Returns 0 If the two operands are equal, 1 If the num1 is larger
- ;* than the num2, -1 otherwise
- ;*/
- Function CompBE64(num1$, num2$)
- For I = 1 To 8
- A = Asc(Mid$(num1$, I))
- If A < 0 Then A = 0
- B = Asc(Mid$(num2$, I))
- If B < 0 Then B = 0
- If A < B Then Return -1
- If A > B Then Return 1
- Next
- Return 0
- End Function
- Print "CompBE64 Tests:"
- Print CompBE64(ZeroBE64$, ZeroBE64$)
- Print CompBE64(ZeroBE64$, OneBE64$)
- Print CompBE64(OneBE64$, ZeroBE64$)
- Print CompBE64(OneBE64$, OneBE64$)
- Print CompBE64("", ZeroBE64$)
- Print CompBE64("", OneBE64$)
- Print CompBE64(ZeroBE64$, "")
- Print CompBE64(OneBE64$, "")
- ;**
- ;* Shift the string left by a number of bits
- ;*
- ;* Shift the bits of num$ bits steps to the left
- ;*
- ;* @param num$ The number to shift
- ;* @param bits Number of bits to shift left
- ;*
- ;* @return This number, shifted left bits bits
- ;*/
- Function ShlBE64$(num$, bits)
- result$ = ""
- H = bits Shr 3
- L = bits And 7
- For I = 1 To 8
- A = Asc(Mid$(num$, I + H))
- If A < 0 Then A = 0
- B = Asc(Mid$(num$, I + H + 1))
- If B < 0 Then B = 0
- result$ = result$ + Chr$((A Shl L) Or (B Shr (8 - L)))
- Next
- Return result$
- End Function
- Print "ShlBE64 Tests:"
- DumpBE64 ShlBE64$(OneBE64$, 0)
- DumpBE64 ShlBE64$(OneBE64$, 1)
- DumpBE64 ShlBE64$(OneBE64$, 2)
- DumpBE64 ShlBE64$(OneBE64$, 4)
- DumpBE64 ShlBE64$(OneBE64$, 8)
- DumpBE64 ShlBE64$(OneBE64$, 16)
- DumpBE64 ShlBE64$(OneBE64$, 32)
- DumpBE64 ShlBE64$(OneBE64$, 63)
- DumpBE64 ShlBE64$(OneBE64$, 64)
- ;**
- ;* Subtraction on string
- ;*
- ;* Difference of num1$ and num2$
- ;*
- ;* @param num1$ Initial number
- ;* @param num2$ Number to subtract
- ;*
- ;* @return Difference of num1$ and num2$
- ;*/
- Function SubBE64$(num1$, num2$)
- result$ = ""
- carry = 0
- For I = 8 To 1 Step -1
- A = Asc(Mid$(num1$, I))
- If A < 0 Then A = 0
- B = Asc(Mid$(num2$, I))
- If B < 0 Then B = 0
- result$ = Chr$((A - B - carry) And 255) + result$
- If A > B
- carry = 0
- ElseIf A < B
- carry = 1
- EndIf
- Next
- Return result$
- End Function
- Print "SubBE64 Tests:"
- DumpBE64 SubBE64$(ZeroBE64$, ZeroBE64$)
- DumpBE64 SubBE64$(OneBE64$, OneBE64$)
- DumpBE64 SubBE64$(OneBE64$, ZeroBE64$)
- DumpBE64 SubBE64$(EncodeBE64$(65536), EncodeBE64$(65535))
- ;**
- ;* Addition on string
- ;*
- ;* Sum of num1$ and num2$
- ;*
- ;* @param num1$ Initial number
- ;* @param num2$ Number to add
- ;*
- ;* @return Sum of num1$ and num2$
- ;*/
- Function AddBE64$(num1$, num2$)
- result$ = ""
- carry = 0
- For I = 8 To 1 Step -1
- A = Asc(Mid$(num1$, I))
- If A < 0 Then A = 0
- B = Asc(Mid$(num2$, I))
- If B < 0 Then B = 0
- result$ = Chr$((A + B + carry) And 255) + result$
- If A + B + carry > 255
- carry = 1
- Else
- carry = 0
- EndIf
- Next
- Return result$
- End Function
- Print "AddBE64 Tests:"
- DumpBE64 AddBE64$(ZeroBE64$, ZeroBE64$)
- DumpBE64 AddBE64$(OneBE64$, OneBE64$)
- DumpBE64 AddBE64$(OneBE64$, ZeroBE64$)
- DumpBE64 AddBE64$(EncodeBE64$(65535), EncodeBE64$(65535))
- DumpBE64 AddBE64$(EncodeBE64$(65536), EncodeBE64$(65535))
- ;**
- ;* Division on string
- ;*
- ;* Quotient of num1$ and num2$
- ;*
- ;* @fixme Currently does not pass tests
- ;*
- ;* @param num1$ Initial number
- ;* @param num2$ Number to subtract
- ;*
- ;* @return Difference of num1$ and num2$
- ;*/
- Function DivBE64(num1$, num2$)
- Result$ = ""
- remainder$ = num1$
- If CompBE64(num2$, "") <> 0
- For lp = 63 To 0 Step -1
- If CompBE64(ShlBE64$(num2$, lp), num1$) <= 0
- num1$ = SubBE64$(num1$, ShlBE64$(num2$, lp))
- Result$ = AddBE64$(Result$, EncodeBE64$(1 Shl lp))
- EndIf
- Next
- remainder$ = num1$
- EndIf
- Return Result$ ;remainder$ also contains remainder
- End Function
- Print "DivBE64 Tests:"
- DumpBE64 DivBE64(ZeroBE64$, OneBE64$)
- DumpBE64 DivBE64(OneBE64$, OneBE64$)
- DumpBE64 DivBE64(EncodeBE64$(12345), OneBE64$)
- DumpBE64 DivBE64(EncodeBE64$(12345), EncodeBE64$(456))
- WaitKey
- End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement