none
How to convert a large hex string to a decimal number RRS feed

Answers

  • Hi 

    As Viorel said,a decimal variable cannot hold such number.It is recommended to put the result in a double type.

    code:

    Imports System.Text
    
    Module Module1
    
        Sub Main()
            Dim str As String = "a59abbe56e057fe10adc39549b20f883e10"
    
            str = Reverse(str)
            Dim strList As List(Of String) = New List(Of String)()
            For i As Integer = 0 To str.Length - 1
                strList.Add(str.Substring(i, 1))
            Next
    
            Dim strArr = strList.ToArray()
            Dim sum As double = 0.0
    
            For i As Integer = 0 To strArr.Length - 1
    
                If strArr(i) = "a" OrElse strArr(i) = "A" Then
                    strArr(i) = "10"
                End If
    
                If strArr(i) = "b" OrElse strArr(i) = "B" Then
                    strArr(i) = "11"
                End If
    
                If strArr(i) = "c" OrElse strArr(i) = "C" Then
                    strArr(i) = "12"
                End If
    
                If strArr(i) = "d" OrElse strArr(i) = "D" Then
                    strArr(i) = "13"
                End If
    
                If strArr(i) = "e" OrElse strArr(i) = "E" Then
                    strArr(i) = "14"
                End If
    
                If strArr(i) = "f" OrElse strArr(i) = "F" Then
                    strArr(i) = "15"
                End If
    
                sum = sum + Convert.ToInt32(strArr(i)) * Math.Pow(16, i)
            Next
            Console.WriteLine(sum.ToString)
    
            Console.ReadLine()
    
        End Sub
        Public Function Reverse(ByVal str2 As String) As String
            Dim sb As StringBuilder = New StringBuilder()
            If String.IsNullOrEmpty(str2) Then
                Throw New ArgumentException("string is null")
            Else
                For i = 0 To str2.Length - 1
                    sb.Append(str2(str2.Length - 1 - i))
                Next
            End If
            Return sb.ToString()
        End Function
    End Module

    Best Regadrs,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Marked as answer by CLG123 Thursday, July 12, 2018 11:55 PM
    • Edited by Alex Li-MSFT Friday, July 13, 2018 1:10 AM
    Thursday, July 12, 2018 6:55 AM
  • Hi Reed

    How can I convert hex?

    dim hex as string="a59abbe56e057fe10adc39549b20f883e10"
    thank you for your help.

    How was that hex string generated in the first place?

    Do you just want to convert the string into decimal digits (not a single decimal number)?

    Dim hex As String = "a59abbe56e057fe10adc39549b20f883e10"
    Dim sb As New Text.StringBuilder
    For i = 0 To hex.Length - 2 Step 2
        If sb.Length > 0 Then sb.Append(" ")
        sb.Append(Integer.Parse(hex.Substring(i, 2), Globalization.NumberStyles.HexNumber))
    Next
    'sb.ToString() = 165 154 187 229 110 5 127 225 10 220 57 84 155 32 248 131 225
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by CLG123 Thursday, July 12, 2018 11:57 PM
    Thursday, July 12, 2018 11:48 AM
    Moderator
  • Hi Viorel

    Thank you very much, I understand.

    Regards

    • Marked as answer by CLG123 Thursday, July 12, 2018 11:55 PM
    Thursday, July 12, 2018 11:49 PM

All replies

  • A hex string is by default an integer (of some size) unless there is some encoding that specifies which bytes represent which parts of the decimal number.  You would have to provide more information about where this hex string comes from and what type of decimal number you expect it to represent.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, July 12, 2018 1:57 AM
    Moderator
  • Hi Reed

    How can I convert hex?

    dim hex as string="a59abbe56e057fe10adc39549b20f883e10"
    thank you for your help.

    Thursday, July 12, 2018 4:15 AM
  • How can I convert hex?

    dim hex as string="a59abbe56e057fe10adc39549b20f883e10"

    A Decimal number needs 16 bytes, or 32 hexadecimal characters, however your example is longer. 

    A decimal variable cannot hold such number.


    Thursday, July 12, 2018 4:57 AM
  • Hi 

    As Viorel said,a decimal variable cannot hold such number.It is recommended to put the result in a double type.

    code:

    Imports System.Text
    
    Module Module1
    
        Sub Main()
            Dim str As String = "a59abbe56e057fe10adc39549b20f883e10"
    
            str = Reverse(str)
            Dim strList As List(Of String) = New List(Of String)()
            For i As Integer = 0 To str.Length - 1
                strList.Add(str.Substring(i, 1))
            Next
    
            Dim strArr = strList.ToArray()
            Dim sum As double = 0.0
    
            For i As Integer = 0 To strArr.Length - 1
    
                If strArr(i) = "a" OrElse strArr(i) = "A" Then
                    strArr(i) = "10"
                End If
    
                If strArr(i) = "b" OrElse strArr(i) = "B" Then
                    strArr(i) = "11"
                End If
    
                If strArr(i) = "c" OrElse strArr(i) = "C" Then
                    strArr(i) = "12"
                End If
    
                If strArr(i) = "d" OrElse strArr(i) = "D" Then
                    strArr(i) = "13"
                End If
    
                If strArr(i) = "e" OrElse strArr(i) = "E" Then
                    strArr(i) = "14"
                End If
    
                If strArr(i) = "f" OrElse strArr(i) = "F" Then
                    strArr(i) = "15"
                End If
    
                sum = sum + Convert.ToInt32(strArr(i)) * Math.Pow(16, i)
            Next
            Console.WriteLine(sum.ToString)
    
            Console.ReadLine()
    
        End Sub
        Public Function Reverse(ByVal str2 As String) As String
            Dim sb As StringBuilder = New StringBuilder()
            If String.IsNullOrEmpty(str2) Then
                Throw New ArgumentException("string is null")
            Else
                For i = 0 To str2.Length - 1
                    sb.Append(str2(str2.Length - 1 - i))
                Next
            End If
            Return sb.ToString()
        End Function
    End Module

    Best Regadrs,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Marked as answer by CLG123 Thursday, July 12, 2018 11:55 PM
    • Edited by Alex Li-MSFT Friday, July 13, 2018 1:10 AM
    Thursday, July 12, 2018 6:55 AM
  • Hi Reed

    How can I convert hex?

    dim hex as string="a59abbe56e057fe10adc39549b20f883e10"
    thank you for your help.

    How was that hex string generated in the first place?

    Do you just want to convert the string into decimal digits (not a single decimal number)?

    Dim hex As String = "a59abbe56e057fe10adc39549b20f883e10"
    Dim sb As New Text.StringBuilder
    For i = 0 To hex.Length - 2 Step 2
        If sb.Length > 0 Then sb.Append(" ")
        sb.Append(Integer.Parse(hex.Substring(i, 2), Globalization.NumberStyles.HexNumber))
    Next
    'sb.ToString() = 165 154 187 229 110 5 127 225 10 220 57 84 155 32 248 131 225
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by CLG123 Thursday, July 12, 2018 11:57 PM
    Thursday, July 12, 2018 11:48 AM
    Moderator
  • Hi Viorel

    Thank you very much, I understand.

    Regards

    • Marked as answer by CLG123 Thursday, July 12, 2018 11:55 PM
    Thursday, July 12, 2018 11:49 PM
  • Hi Alex

    This is the method I need,but I am afraid you have made a mistake.

     Dim sum As Int64 = 0.0?

    Regards

    Thursday, July 12, 2018 11:52 PM
  • Hi Reed

    This is also a good method.I will collect it for the next time I use it.

    Regadrs

    Thursday, July 12, 2018 11:57 PM