C Lampo
C Lampo

Reputation: 81

Any way to convert from Double to Hex in vb?

I want to add a text box where a user can input a number. No matter how large or small the number, it needs to be stored as a double. Then with a click button, the hex equivalent will be displayed in a second text box. Bonus points if you can show me how to take the 16 byte hex and change it to 4 variables with 4 bytes each.

For example, user enters 1234.56 in textbox1. Clicks button 1, and textbox2 displays the hex equivilent "40934A3D70A3D70A" Then take that string, and extract to 4 different 4-byte strings so str1=1093, str2=4A3D, str3=70a3, str4=d70A.

Upvotes: 0

Views: 1562

Answers (5)

M.Hassan
M.Hassan

Reputation: 11032

try it: DoubleToHex

          //dashseparator 0 /2/4
        public string DoubleToHex(double d, bool reverse = false, int dashSeparator = 0)
        {
            byte[] bytes = BitConverter.GetBytes(d);
            if (reverse) bytes = bytes.Reverse().ToArray();
            var hex = BitConverter.ToString(bytes);
            var hex4 = "";
            if (dashSeparator == 2) return hex;
            if (dashSeparator == 4)
            {
                hex = hex.Replace("-", "");
                hex = Regex.Replace(hex, ".{4}", "$0-").TrimEnd('-');
                return hex;
            }
            return hex.Replace("-", "");
        }

sample Output:

Double: 1234.56
Hex:                  0AD7A3703D4A9340
Hex in Reverse order: 40934A3D70A3D70A
Hex in Reverse order separate by 2 digits: 40-93-4A-3D-70-A3-D7-0A
Hex in Reverse order separate by 4 digits: 4093-4A3D-70A3-D70A

you can :

-control the generated Hex to be displayed in order/ reverse order.

-Add dash separator by 0 (no separator) / 2 /4 digits

Edit:

Vb.Net Version

Converting Code to Vb.Net

'dashseparator 0 /2/4
Public Function DoubleToHex(d As Double, Optional reverse As  Boolean = False, Optional dashseparator As Integer = 0) As String
 Dim bytes As Byte() = BitConverter.GetBytes(d)
 If reverse Then
    Array.Reverse(bytes)
 End If
 Dim hex = BitConverter.ToString(bytes)
 Dim hex4 = ""
 If dashseparator = 2 Then
    Return hex
 End If
 If dashseparator = 4 Then
    hex = hex.Replace("-", "")
    hex = Regex.Replace(hex, ".{4}", "$0-").TrimEnd("-"C)
    Return hex
 End If
 Return hex.Replace("-", "")
End Function

Upvotes: 0

ventiseis
ventiseis

Reputation: 3099

Translation of Dmitry Bychenko answer to VB.NET:

Imports SplitValue = System.Tuple(Of String, String, String, String)

Module Module1

    Function DoubleToByteArray(ByVal AValue As Double) As Byte()
        Return BitConverter.GetBytes(AValue).Reverse().ToArray()
    End Function

    Function SplitByteArray(ByRef AValue As Byte()) As SplitValue
        Dim StringValue As String = String.Join("", From AByte In AValue Select AByte.ToString("X2"))
        Return New SplitValue(StringValue.Substring(0, 4), StringValue.Substring(4, 4), StringValue.Substring(8, 4), StringValue.Substring(12, 4))
    End Function

    Sub Main()
        Dim Result As SplitValue
        Result = SplitByteArray(DoubleToByteArray(1234.56))
        Console.WriteLine(Result)
        Console.ReadLine()
    End Sub

End Module

Output:

(4093, 4A3D, 70A3, D70A)

Upvotes: 0

hoodaticus
hoodaticus

Reputation: 3880

This is the most efficient answer you're going to get:

unsafe static void Main()
{
    var value = 1234.56;
    var pLong = (long*)&value;
    var fullString = (*pLong).ToString("X");
    var pShort = (short*)pLong;
    short value0 = *pShort, value1 = *(pShort + 1), value2 = *(pShort + 2), 
          value3 = *(pShort + 3);
    string s0 = value0.ToString("X"), s1 = value1.ToString("X"), s2 = value2.ToString("X"), 
           s3 = value3.ToString("X");

    Debug.Print(fullString);
    Debug.Print(s0);
    Debug.Print(s1);
    Debug.Print(s2);
    Debug.Print(s3);
}

Output: 40934A3D70A3D70A

D70A

70A3

4A3D

4093

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186678

Are you looking for BitConverter? C# implementation:

double source = 1234.56;

// 40934A3D70A3D70A
string result = string.Concat(BitConverter
  .GetBytes(source)
  .Reverse() 
  .Select(b => b.ToString("X2")));

Having got result, extract its parts with Substring:

string str1 = result.Substring(0, 4);
string str2 = result.Substring(4, 4);
string str3 = result.Substring(8, 4); 
string str4 = result.Substring(12, 4);

Upvotes: 1

Igor Damiani
Igor Damiani

Reputation: 1927

If you are looking for a C# implementation, try this:

    static void Main(string[] args)
    {
        var number = 1234.56;
        string hex = DoubleToHex(number);
        string part1 = hex.Substring(0, 4);
        string part2 = hex.Substring(4, 4);
        string part3 = hex.Substring(8, 4);
        string part4 = hex.Substring(12, 4);
    }

    internal static string DoubleToHex(double value)
    {
        var b = BitConverter.GetBytes(value).Reverse();
        string result = string.Join(string.Empty, b.Select(i => i.ToString("X2")).ToArray());

        return result;
    }

Upvotes: 0

Related Questions