vb.net 如何在VB.NET中将字符串转换为十六进制?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10504034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:05:50  来源:igfitidea点击:

How do you convert a string into hexadecimal in VB.NET?

vb.net

提问by Mark Chai

How do I convert a string from a textbox into hexadecimal?

如何将文本框中的字符串转换为十六进制?

I have only found ways in C#. Does VB.NET have the ability to do such a thing? If it can then I'd like to know how to convert string to hex and hex to string.

我只在 C# 中找到了方法。VB.NET 有能力做这样的事情吗?如果可以,那么我想知道如何将字符串转换为十六进制并将十六进制转换为字符串。

回答by Habib

Dim val As String
val = "10"
Dim hexVal As Integer
hexVal = Convert.ToInt32(val, 16) //16 specifies the base
Console.WriteLine(hexVal)

This will display 16 which is the integer equivalent of the hexadecimal string "10".

这将显示 16,它是十六进制字符串“10”的等效整数。

回答by Cary Bondoc

Tried and tested code

尝试和测试的代码

Create this function by copy pasting it.

通过复制粘贴创建此函数。

Function StringToHex(ByVal text As String) As String
    Dim hex As String
    For i As Integer = 0 To text.Length - 1
        hex &= Asc(text.Substring(i, 1)).ToString("x").ToUpper
    Next
    Return hex
End Function

Use it like this

像这样使用它

Debug.WriteLine(StringToHex("sim0n"))

Source

来源

回答by Phil

Public Function StrToHex(ByRef Data As String) As String
    Dim sVal As String
    Dim sHex As String = ""
    While Data.Length > 0
        sVal = Conversion.Hex(Strings.Asc(Data.Substring(0, 1).ToString()))
        Data = Data.Substring(1, Data.Length - 1)
        sHex = sHex & sVal
    End While
    Return sHex
End Function

回答by Rasmus S?borg

You can convert an integer to a hexdecimal number easily by doing:

您可以通过执行以下操作轻松将整数转换为十六进制数:

Convert.ToInt32(15, 16)

And to convert it back to an integer, you can do:

并将其转换回整数,您可以执行以下操作:

Integer.Parse("15f", System.Globalization.NumberStyles.HexNumber)

回答by Tilak

To convert into hexadecimal, use Convert.ToInt32(val, 16). Convert.ToInt32 supports limited bases, 2, 8, 10, and 16.

要转换为十六进制,请使用Convert.ToInt32(val, 16). Convert.ToInt32 支持有限的基数,2、8、10 和 16。

To convert into any base, use:

要转换为任何基数,请使用:

Public Shared Function IntToString(value As Integer, baseChars As Char()) As String
    Dim result As String = String.Empty
    Dim targetBase As Integer = baseChars.Length

    Do
        result = baseChars(value Mod targetBase) + result
        value = value / targetBase
    Loop While value > 0

    Return result
End Function

The above function comes from this question. The C# to VB conversion was done using this.

上述功能来自这个问题。C# 到 VB 的转换是使用这个完成的。

回答by miroxlav

Short and effective expression to display all characters of String sin hexadecimal formcan be written using LINQ:

可以使用 LINQ 编写以十六进制形式显示字符串所有字符的s简短有效表达式

String.Join(" ", s.Select(Function(c) Conversion.Hex(AscW(c)).PadLeft(4, "0")).ToArray()))

Example:

例子:

For string ? fixit gives string 25BA 0020 0066 0069 0078.

对于 string? fix它给出 string 25BA 0020 0066 0069 0078

Enjoy!

享受!

Please keep in mind this is Unicode-enabled, returning 4-digit hexadecimal value for every character, because old plain Non-Unicode ASCII is dead and you should no longer rely on it in any application.

请记住,这是启用 Unicode 的,为每个字符返回 4 位十六进制值,因为旧的纯非 Unicode ASCII 已死,您不应再在任何应用程序中依赖它。

回答by Evert

In my humble opinion the code of Tilak seems OK, but is not.
The rounding of value / targetBasegives results that are too large.
By using Fix()the resulting integers will be as they should be.
I compliment Tilak for finding such a neat solution for the question.

在我看来,Tilak 的代码似乎还可以,但事实并非如此。value / targetBase
的舍入会给出太大的结果。
通过使用Fix()产生的整数将是它们应该的样子。
我称赞蒂拉克为这个问题找到了如此巧妙的解决方案。

In the accompanying code I will show how functions like IntToString can be tested easily.

在随附的代码中,我将展示如何轻松测试 IntToString 等函数。

The expected message in the message box is:

消息框中的预期消息是:

The old results are;
0 1 10 101 100 101 1010 1001 1000 1001 1010
0 1 1X 10 11 1XX 1X0 1X1 10X 100 101
0 1 X 1y 10 11 XX Xy X0 X1 XX
0 1 X 1y 1z 10 11 1X Xy Xz X0
The new results are;
0 1 10 11 100 101 110 111 1000 1001 1010
0 1 X 10 11 1X X0 X1 XX 100 101
0 1 X y 10 11 1X 1y X0 X1 XX
0 1 X y z 10 11 1X 1y 1z X0
The new results are better IMHO.

旧的结果是;
0 1 10 101 100 101 1010 1001千1001 1010
0 1 1×10 11 1XX 1X0 1X1 10X 100 101
0 1 X 1Y 10 11 XX XY X0 X1 XX
0 1 X 1Y 1Z 10 11 1X XY基XZ X0
新的结果是;
0 1 10 11 100 101 110 111 1000 1001 1010
0 1 X 10 11 1X X0 X1 XX 100 101
0 1 X y 10 11 1X 1y X0 X1 XX
1z 1 0 X 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 1 0 1 0 1 1 1 0 1 X
更好的结果X更好

Public Sub test()
    Dim numberCharacters As String = "01Xyz"
    Dim messageText As String = "The old results are;" & vbNewLine
    For loopCount As Integer = 1 To 2
        If loopCount = 2 Then messageText &= "The new results are;" & vbNewLine
        For baseLength As Integer = 2 To 5
            Dim baseCharacters As Char() = _
                Strings.Left(numberCharacters, baseLength).ToArray
            For integerValue As Integer = 0 To 10
                Dim resultText As String = _
                    Me.IntToString(integerValue, baseCharacters, loopCount = 2)
                messageText &= resultText & " "
            Next
            messageText &= vbNewLine
        Next
    Next
    Call MsgBox(berichtTekst & "The new results are better IMHO.")
End Sub

Public Function IntToString(value As Integer, baseChars As Char(), _
        Optional newCode As Boolean = False) As String
    Dim result As String = String.Empty
    Dim targetBase As Integer = baseChars.Length
    Do
        result = baseChars(value Mod targetBase) & result
        If newCode Then ' Improved code
            value = Fix(value / targetBase)
        Else ' Original code
            value = value / targetBase
        End If
    Loop While value > 0
    Return result
End Function

Hopefully this will help others.

希望这会帮助其他人。

I have more than 25 years experience as a programmer, mainly in RPG, Cobol, Synon, CL and Basic. I also know some Delphi, Pascal and C#. I am sure I can be a help to this community.
It makes me sad that I cannot comment to answers. Hopefully someone will add me some points, so that I can more easily help others from now on.
Because of this I add my comment to the answer of Tilak, dated may 8, 2012, as an answer. This is the first and hopefully the only time that I resort to this. Sorry for that, but I know no other way.

我有超过 25 年的程序员经验,主要在 RPG、Cobol、Synon、CL 和 Basic。我也知道一些 Delphi、Pascal 和 C#。我相信我可以帮助这个社区。
我无法对答案发表评论,这让我感到难过。希望有人能给我加分,这样我以后就可以更轻松地帮助别人了。
因此,我将我的评论添加到了 2012 年 5 月 8 日 Tilak 的答案中,作为答案。这是我第一次也是唯一一次求助于此。很抱歉,但我不知道其他方式。