-1

The following code converts hex string to numeric values and it works fine with small hex numbers such as 1f. But, it doesn't work with large numbers such as "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"because the int32 can't have that much larger/small number. But there should be another way of bypassing these limits right? can someone shed some light here, or any search keywords are welcome to search through google etc as I couldn't find any methods to convert large hex numbers.

Public Class Form1

    Dim data As String = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"
    Dim result1 As String = Nothing


    Public Function hexToNumeric(ByVal inputHexstring As String)
        Dim val As Integer

        val = Convert.ToInt32(inputHexstring, 16)
        Return val
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        result1 = hexToNumeric(data)
    End Sub
End Class
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
Pretty_Girl5
  • 49
  • 1
  • 1
  • 7
  • 1
    There is *always* going to be a limit. You can use an `Int64` but The max value is still `9,223,372,036,854,775,807`; that is, hexadecimal `0x7FFFFFFFFFFFFFFF` so it won't really help - what on earth do you intend to do with this massive number if you could convert it to an integer? – Matt Wilko Jun 22 '16 at 10:50
  • @MattWilko : I'm trying to mimic the functionality of this python code `Result1 = data.decode('hex')[::-1]` to vb.net code. As I know, that code decodes the `hex string` in `data` variable and then reverse the `bits`. So, I'm first trying to code in vb.net to decode that `hex string` to `numeric` and later I'll reverse the `bits` . – Pretty_Girl5 Jun 22 '16 at 10:55
  • what I don't understand is `python` can decode enormous hex numbers and `vb.net` can't decode more than `int64`? – Pretty_Girl5 Jun 22 '16 at 10:56
  • Possible duplicate of [Convert hex value string to Binary string](http://stackoverflow.com/questions/37898152/convert-hex-value-string-to-binary-string) – Blackwood Jun 22 '16 at 13:58

2 Answers2

1

You should look at the BigInteger Structure, see https://msdn.microsoft.com/en-us/library/system.numerics.biginteger(v=vs.110).aspx

You can convert from a byte array to the BigInteger, but watch out for the following: "The BigInteger structure expects the individual bytes in a byte array to appear in little-endian order (that is, the lower-order bytes of the value precede the higher-order bytes)."

AndrewW
  • 11
  • 4
  • I even can't test it as `imports System.Numeric` namespace is not a valid namespace in my VisualStudio 2012. I only have `System.Nullable` `System.nullable(ofT)`, `System.NullReferenceException` which starts with letter `N` – Pretty_Girl5 Jun 22 '16 at 12:34
  • You need .Net Framework 4.5 or 4.6 and add a reference in your project. – AndrewW Jun 22 '16 at 12:50
  • `@Andrew` : I already added the namespace in the code when I test it ... and how to check if mine is .net framework 4.5 ? – Pretty_Girl5 Jun 22 '16 at 13:50
  • The simplest way to check for .Net version is from a Visual Studio Command Prompt and enter clrver. Also, adding the namespace in code is one part of using an assembly, you also need to show where the assembly comes from. This is done by setting a new reference for your project. – AndrewW Jun 22 '16 at 14:23
  • `@andrew` : now it's successfully added as a reference now the namespace is available in the project, but still it throws exception when I try to execute. `Dim val As BigInteger` `val = Convert.ToInt32(inputHexstring, 16)` - `Value was either too large or too small for a UInt32.` – Pretty_Girl5 Jun 22 '16 at 14:33
  • `You need .Net Framework 4.5 or 4.6` No, the MSDN link posted clearly shows it is available in Net 4. You would have to add a reference to `System.Numerics` – Ňɏssa Pøngjǣrdenlarp Jun 22 '16 at 15:16
  • @pretty-girl5 for the convert you are still using the Int32 which has the limit you are trying to avoid. See the response from chris-dunaway on how to avoid this using the BigInteger Parse method. – AndrewW Jun 23 '16 at 08:02
0

You can use BigInteger along with Parse to duplicate the functionality in your sample.

Imports System.Numerics

Public Shared Function hexToNumeric(ByVal inputHexstring As String) As BigInteger
    Dim val As BigInteger

    val = BigInteger.Parse(inputHexstring, Globalization.NumberStyles.AllowHexSpecifier)

    Return val
End Function

Public Shared Sub Main()
    Dim data As String = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"

    Console.WriteLine(hexToNumeric(data))
    Console.ReadLine()
End Sub

Result is

6860217587554922525607992740653361396256930700588249487127
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48