Sha256: 2f08481ce24e718368389df0b7aed56eab09c54194b4506221fb569ec6bc26c2
Contents?: true
Size: 705 Bytes
Versions: 396
Compression:
Stored size: 705 Bytes
Contents
Public Class Binary Private ReadOnly value As String Public Sub New(value As String) Me.value = value End Sub Public Function ToDecimal() As Integer If IsNotValidBinary() Then Return 0 End If Return value.Select(Function(c, i) Integer.Parse(c.ToString()) * TwoToThePowerOf(value.Length - i - 1)).Sum() End Function Private Function IsNotValidBinary() As Boolean Return Not value.All(Function(x) Char.IsDigit(x) AndAlso Integer.Parse(x.ToString()) < 2) End Function Private Shared Function TwoToThePowerOf(power As Integer) As Integer Return CInt(Math.Truncate(Math.Pow(2, power))) End Function End Class
Version data entries
396 entries across 396 versions & 1 rubygems