vb.net 将十六进制值字符串转换为二进制字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37898152/
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-17 19:57:37  来源:igfitidea点击:

Convert hex value string to Binary string

vb.netvisual-studio-2012

提问by Pretty_Girl4

I want to convert 000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717hex value to it's binary format (to a string of binary), but following code throws an exception if either value being too big or too small. Why is that?

我想将000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717十六进制值转换为它的二进制格式(转换为二进制字符串),但是如果值太大或太小,以下代码会引发异常。这是为什么?

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim binstring As String
        Dim hexstring As String = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"
        binstring = Convert.ToString(Convert.ToInt32(hexstring, 16), 2)

        T5.Text = binstring
    End Sub

回答by user94559

Convert.ToInt32converts to, unsurprisingly, an Int32.

Convert.ToInt32毫不奇怪,转换为Int32.

The maximum value of an Int32is Int32.MaxValue, which is 2,147,483,647.

的最大值Int32Int32.MaxValue,这是2147483647。

The number in your code, 0x117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717, is much larger than that (6,860,217,587,554,922,525,607,992,740,653,361,396,256,930,700,588,249,487,127 in decimal), so it doesn't come close to fitting.

在你的代码,0x117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717数量比(十进制6,860,217,587,554,922,525,607,992,740,653,361,396,256,930,700,588,249,487,127)大得多,所以它不会来接近拟合。

EDIT

编辑

You didn't ask for help actually converting this, but here's a hint: each hex digit represents four binary digits (because 16 is 2^4). You can convert each digit of the hexadecimal individually and just concatenate them. In other words, 0xF1 = 11110001 in binary, because 0xF = 1111 and 0x1 = 0001. Just be careful to keep the trailing zeros you need.

您实际上并没有寻求帮助来转换它,但这里有一个提示:每个十六进制数字代表四个二进制数字(因为 16 是 2^4)。您可以单独转换十六进制的每个数字并将它们连接起来。换句话说,二进制的 0xF1 = 11110001,因为 0xF = 1111 和 0x1 = 0001。小心保留你需要的尾随零。

回答by Blackwood

As I said in my comment on your question, your hex string is too long to convert to a 32-bit integer, which your code is trying to do. I would approach this by looping through the characters of the hex string and converting each to a binary string of length 4 (padded on the left with "0").

正如我在对您的问题的评论中所说的,您的十六进制字符串太长而无法转换为您的代码正在尝试执行的 32 位整数。我会通过循环遍历十六进制字符串的字符并将每个字符转换为长度为 4 的二进制字符串(在左侧用“0”填充)来解决这个问题。

Dim hexstring As String = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"
Dim bin As New Text.StringBuilder
For Each ch As Char In hexstring
    bin.Append(Convert.ToString(Convert.ToInt32(ch, 16), 2).PadLeft(4, "0"c))
Next
T5.Text = bin.ToString