0

How are you?

I wrote a program manipulating big binary chains (string variables). This said manipulation requires me to store my chains in a variable so I can use them as numbers. The only variable type that I have found big enough to store such lengthy numbers is BigInteger (we are talking 1.0E100+).

I would like to use something like:

val = BigInteger.Parse(bin, 2)

But the second parameter needed is a NumberStyles object, which can only refer to a NumberStyles.HexNumber.

Is there a simple/optimal way to do this?

Thank you very much. :)

user7831458
  • 83
  • 1
  • 8
  • Can't you use a BitArray for it? – David Sdot May 10 '17 at 08:24
  • For what purpose? – user7831458 May 10 '17 at 08:32
  • For your big binary chains :) – David Sdot May 10 '17 at 08:33
  • How exactly? You mean slicing the string into byte (8-bit) chunks and store them into arrays? How can I use them after and parse them into BigInteger values? – user7831458 May 10 '17 at 09:07
  • I think that you might have to process the binary `String` in 64-character chunks. You can convert a chunk to a `Long` and convert that to `BigInteger`. You can then shift that 64 bits to the left and then add the next converted chunk. – jmcilhinney May 10 '17 at 09:25
  • Isn't there for example a way to directly convert my binary strings into hexadecimal stringsnwithout needing to store them somewhere (and causing an overflow)? BigInteger.Parse can be used with Hex strings). – user7831458 May 10 '17 at 09:31
  • Can you show an example of that string? Is it stored as a string or is it stored as bytes and you create a hex string out of the bytes? – David Sdot May 10 '17 at 09:35
  • There's no direct way to convert from a binary `String` to a hexadecimal `String`. You'd have to process the `String` in chunks and convert to an actual number (`Byte`, `Short`, `Integer` or `Long`) and then convert the number to a hex `String` and concatenate. – jmcilhinney May 10 '17 at 09:36

1 Answers1

0

This converts a binary string to BigInteger in 8 bit chunks. It assumes that the binary string represents a positive number.

Private Function BinToBI(ByRef binstr As String) As BigInteger
    Dim t As New List(Of Byte)
    Dim s As String
    Dim idx As Integer = binstr.Length
    Do While idx > 0
        'get 8 bits
        If idx >= 8 Then
            s = binstr.Substring(idx - 8, 8)
        Else
            s = binstr.Substring(0, idx).PadLeft(8, "0"c)
        End If
        'convert to byte and add to list
        Dim b As Byte = Convert.ToByte(s, 2)
        t.Add(b)
        idx -= 8
    Loop
    'force to positive
    If t(t.Count - 1) >= 128 Then
        t.Add(0)
    End If
    Dim rv As New BigInteger(t.ToArray)
    Return rv
End Function

for testing

    Dim d As Double = 1.0E+101
    Debug.WriteLine(d.ToString("n2"))
    Dim bi As BigInteger
    ' Dim bin As String = "1111111111111111111111111111111" 'Integer.MaxValue
    ' Dim bin As String = "111111111111111111111111111111111111111111111111111111111111111" 'Long.MaxValue
    Dim bin As String = "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110"
    bi = BinToBI(bin)
    Debug.WriteLine(bi.ToString("n2"))

This was not well tested but should give you some ideas.

dbasnett
  • 11,334
  • 2
  • 25
  • 33
  • Thank you so much. Works like a charm. So I understand that there is no way to parse a binary string to BigInteger then. – user7831458 May 11 '17 at 07:38