Converting huge numbers for Decimal to Hexadecimal and visa versa
-
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 -
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? ThanksHi, 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, JohanMy advice is free, and you may get what you paid for.
-
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? ThanksI 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)).ToStringMsgBox(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