Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Converting huge numbers for Decimal to Hexadecimal and visa versa

Converting huge numbers for Decimal to Hexadecimal and visa versa

Scheduled Pinned Locked Moved Visual Basic
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fu Manchu
    wrote on last edited by
    #1

    Hi all,:) i have been trying to create some function that take the Decimal datatype to convert decimal to hexadecimal and the reverse. my code at present uses the long datatype which go up to 9.2 E+18. Currently i am working with larger numbers. This is my code: Private Function ConvertDecimalToHexadecimal(ByVal number As Decimal) Dim converted As String Dim value As Long = Long.Parse(number) converted = value.ToString("X") Return converted End Function Private Function ConvertHexadecimalToDecimal(ByVal number As String) Dim txt As String Dim value As Decimal Dim converted As String txt = UCase(Trim(number)) If txt.StartsWith("&H") Then txt = txt.Substring(2) value = long.Parse(txt, Globalization.NumberStyles.HexNumber) converted = value.ToString() Return converted End Function Is there some way of making this work for numbers up to the decimal datatype of 1.79 E+308 or if anyone knows of another way? Thanks

    J T 2 Replies Last reply
    0
    • F Fu Manchu

      Hi all,:) i have been trying to create some function that take the Decimal datatype to convert decimal to hexadecimal and the reverse. my code at present uses the long datatype which go up to 9.2 E+18. Currently i am working with larger numbers. This is my code: Private Function ConvertDecimalToHexadecimal(ByVal number As Decimal) Dim converted As String Dim value As Long = Long.Parse(number) converted = value.ToString("X") Return converted End Function Private Function ConvertHexadecimalToDecimal(ByVal number As String) Dim txt As String Dim value As Decimal Dim converted As String txt = UCase(Trim(number)) If txt.StartsWith("&H") Then txt = txt.Substring(2) value = long.Parse(txt, Globalization.NumberStyles.HexNumber) converted = value.ToString() Return converted End Function Is there some way of making this work for numbers up to the decimal datatype of 1.79 E+308 or if anyone knows of another way? Thanks

      J Offline
      J Offline
      Johan Hakkesteegt
      wrote on last edited by
      #2

      Hi, According to Microsoft's .NET documentation, you'll have to either use the Double type instead of Long or round you numbers to a more manageable size, before handling them. On a less helpful note, you could make your function a little smaller like so: Private Function ConvertDecimalToHexadecimal(ByVal number As Decimal) As String Return Double.Parse(number).ToString("X") End Function Private Function ConvertHexadecimalToDecimal(ByVal number As String) As String If UCase(Trim(number)).StartsWith("&H") Then number = number.Substring(2) Return Double.Parse(number, Globalization.NumberStyles.HexNumber).ToString End Function Hope it helps, Johan

      My advice is free, and you may get what you paid for.

      1 Reply Last reply
      0
      • F Fu Manchu

        Hi all,:) i have been trying to create some function that take the Decimal datatype to convert decimal to hexadecimal and the reverse. my code at present uses the long datatype which go up to 9.2 E+18. Currently i am working with larger numbers. This is my code: Private Function ConvertDecimalToHexadecimal(ByVal number As Decimal) Dim converted As String Dim value As Long = Long.Parse(number) converted = value.ToString("X") Return converted End Function Private Function ConvertHexadecimalToDecimal(ByVal number As String) Dim txt As String Dim value As Decimal Dim converted As String txt = UCase(Trim(number)) If txt.StartsWith("&H") Then txt = txt.Substring(2) value = long.Parse(txt, Globalization.NumberStyles.HexNumber) converted = value.ToString() Return converted End Function Is there some way of making this work for numbers up to the decimal datatype of 1.79 E+308 or if anyone knows of another way? Thanks

        T Offline
        T Offline
        TwoFaced
        wrote on last edited by
        #3

        I couldn't find any built in functions to do such large conversions. However, I had some code to convert Hex to Dec and vice versa. I made some modifications to ensure the conversion would work with the largest decimal number allowed. Here is the code which I believe works perfectly. If you discover a problem let me know. Hope this helps.

        Public Class Form1
        'Test conversion of the largest allowed decimal value
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim msg As String = "MaxDecimal: " & Decimal.MaxValue.ToString & _
        vbCrLf & "MaxDecimal as Hex: " & DecToHex(Decimal.MaxValue).ToUpper & _
        vbCrLf & "Hex converted back: " & HexToDec(DecToHex(Decimal.MaxValue)).ToString

            MsgBox(msg)
        End Sub
        
        '---MAIN CONVERSION FUNCTIONS---
        
        'Converts a decimal type to it's Hex value
        'Does not support fractional numbers, so don't pass them :)
        'Actually they just get rounded so nothing to worry about.
        Private Function DecToHex(ByVal number As Decimal) As String
            number = Decimal.Round(number)
            Dim result As New System.Text.StringBuilder
        
            Do While number > 0
                Dim remainder As Integer = number Mod 16
        
                number = number / 16D
                number = Decimal.Truncate(number)
                result.Insert(0, DecToHexChar(remainder))
            Loop
            Return result.ToString
        End Function
        
        'Converts a string of Hex characters to it's decimal equivalent
        'Does not provide any validation.  String must have nothing but
        'valid Hex values.  Although no errors should occur.
        Private Function HexToDec(ByVal hex As String) As Decimal
            Dim result As Decimal
        
            For i As Decimal = 0 To hex.Length - 1
                Dim chr As Char = hex.Chars(i)
                result += (HexCharToDec(chr) \* Pow(16, hex.Length - 1 - i))
            Next
            Return result
        End Function
        
        '---HELPER FUNCTIONS---
        
        'Converts a Decimal value 0 to 15 to it's Hex character equivalent
        'If number is anything else it returns String.Empty
        Private Function DecToHexChar(ByVal number As Integer) As String
            If number < 10 Then Return number
            Select Case number
                Case 10 : Return "A"
                Case 11 : Return "B"
                Case 12 : Return "C"
                Case 13 : Return "D"
                Case 14 : Return "E"
                Case 15 : Return "F"
            End Select
            Return String
        
        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups