Posts Tagged ‘functions’

Base64/sexatrigesimal encoding/decoding in VBA/VB6/Visual Basic

Sunday, October 10th, 2010

Here is an implementation of Base 36 enconding/decoding functions is VB6.

'Convert positive integer to a base36 string.
Function base36encode(ByRef number As Long) As String

    Dim alphabet As String
    alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    If number = 0 Then
        base36encode = "0"
        Exit Function
    End If
    base36encode = vbNullString
    Do While number <> 0
        base36encode = Mid(alphabet, number Mod 36 + 1, 1) & base36encode
        number = number \ 36
    Loop

End Function

'Convert base36 string to positive integer.
Function base36decode(ByRef base36 As String) As Long

    Dim alphabet As String
    alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    base36decode = InStr(1, alphabet, Right(base36, 1), vbTextCompare) - 1 'get the last character
    For i = Len(base36) - 1 To 1 Step -1
        base36decode = base36decode + 36 ^ (Len(base36) - i) * (InStr(1, alphabet, Mid(base36, i, 1), vbTextCompare) - 1)
    Next i

End Function

Related posts:

  1. Longest Common Subsequence implemented in VBA (Visual Basic for Applications)
  2. Optimizing/faster String Concatenation in VBA
  3. Generic file selection window function in VBA

PHP Factorial and Combination functions

Friday, June 11th, 2010

A quick post on two functions for PHP that provide factorial and combination support.

function factorial($n) {
	if ($n <= 1) {
		return 1;
	} else {
		return factorial($n - 1) * $n;
	}
}

function combinations($n, $k) {
	//note this defualts to 0 if $n < $k
	if ($n < $k) {
		return 0;
	} else {
		return factorial($n)/(factorial($k)*factorial(($n - $k)));
	}
}

Related posts:

  1. Rigid file manipulation functions for VBA/VBS
  2. RSS feed aggregator/combiner in PHP with Magpie RSS (v2)
  3. RSS feed aggregator/combiner in PHP