| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 
 | Imports System.Numerics
Imports System.Text
 
Module Module1
 
    Sub Main()
        Dim fromBinary = Function(str As String) As BigInteger
                             Dim result = BigInteger.Zero
                             Dim bit = ""
                             For i = 0 To str.Length - 1
                                 result *= 2
                                 If str(i) = "1"c Then result += 1
                             Next
                             Return result
                         End Function
 
        Dim fcts As New Dictionary(Of String, Func(Of BigInteger, String)) From {
            {"Lord - Nelson", Function(number As BigInteger) As String
                                  Dim result = ""
                                  Dim tmp = BigInteger.Zero
                                  Do
                                      tmp = number / 2
                                      result &= (number - (tmp * 2)).ToString
                                      number = tmp
                                  Loop Until number = 0
                                  Return StrReverse(result)
                              End Function},
            {"worm83", Function(number As BigInteger) number.ToByteArray.Select(Function(x) Convert.ToString(Convert.ToInt32(String.Format("{0:X2}", x), 16), 2)).Aggregate(New StringBuilder, Function(sb, current) sb.Append(current)).ToString},
            {"Sehnsucht", Function(number As BigInteger) number.ToByteArray.Aggregate(New StringBuilder, Function(builder, x) builder.Append(Convert.ToString(x, 2)), Function(builder) builder.ToString)}
        }
 
        For Each sample As BigInteger In {-255, -128, -127, 127, 128, 255}
            For Each fct In fcts
                Console.WriteLine("{0,-13} : {1,4} => {2,-16} => {3}", fct.Key, sample, fct.Value(sample), fromBinary(fct.Value(sample)))
            Next
            Console.Write(New String("-"c, Console.WindowWidth))
        Next
    End Sub
 
End Module |