在vb中从Double转换为Hex的任何方法?

时间:2016-08-01 19:31:26

标签: vb.net

我想添加一个用户可以输入数字的文本框。无论数量有多大或多小,都需要存储为双精度数。然后使用单击按钮,十六进制等效项将显示在第二个文本框中。如果您可以告诉我如何取16字节十六进制并将其更改为4个变量,每个变量4个字节,则可获得奖励积分。

例如,用户在textbox1中输入1234.56。单击按钮1,textbox2显示十六进制等效" 40934A3D70A3D70A"然后取出该字符串,并提取到4个不同的4字节字符串,因此str1 = 1093,str2 = 4A3D,str3 = 70a3,str4 = d70A。

5 个答案:

答案 0 :(得分:1)

您在寻找BitConverter吗? C#实施:

double source = 1234.56;

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

获得result后,使用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);

答案 1 :(得分:0)

如果您正在寻找C#实现,请尝试以下方法:

    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;
    }

答案 2 :(得分:0)

这是您将获得的最有效的答案:

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);
}

输出: 40934A3D70A3D70A

D70A

70A3

4A3D

4093

答案 3 :(得分:0)

试一试: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("-", "");
        }

示例输出:

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
你可以:

- 控制生成的Hex以按顺序/反向顺序显示。

- 添加短划线分隔符0(无分隔符)/ 2/4位数

修改

Vb.Net版

将代码转换为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

答案 4 :(得分:0)

Dmitry Bychenko的翻译回答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

输出:

(4093, 4A3D, 70A3, D70A)