35 lines
794 B
Plaintext
35 lines
794 B
Plaintext
' version 25-06-2015
|
|
' compile with: fbc -s console
|
|
|
|
Sub calc_entropy(source As String, base_ As Integer)
|
|
|
|
Dim As Integer i, sourcelen = Len(source), totalchar(255)
|
|
Dim As Double prop, entropy
|
|
|
|
For i = 0 To sourcelen -1
|
|
totalchar(source[i]) += 1
|
|
Next
|
|
|
|
Print "Char count"
|
|
For i = 0 To 255
|
|
If totalchar(i) = 0 Then Continue For
|
|
Print " "; Chr(i); Using " ######"; totalchar(i)
|
|
prop = totalchar(i) / sourcelen
|
|
entropy = entropy - (prop * Log (prop) / Log(base_))
|
|
Next
|
|
|
|
Print : Print "The Entropy of "; Chr(34); source; Chr(34); " is"; entropy
|
|
|
|
End Sub
|
|
|
|
' ------=< MAIN >=------
|
|
|
|
calc_entropy("1223334444", 2)
|
|
Print
|
|
|
|
' empty keyboard buffer
|
|
While InKey <> "" : Wend
|
|
Print : Print "hit any key to end program"
|
|
Sleep
|
|
End
|