将十六进制值字符串转换为二进制字符串 [英] Convert hex value string to Binary string
问题描述
我想将 000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717
十六进制值转换为它的二进制格式(到一个二进制字符串),但如果太小或太小会抛出一个太大的异常这是为什么?
I want to convert 000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717
hex 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?
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
推荐答案
正如我在对您的问题的评论中所说的,您的十六进制字符串太长而无法转换为您的代码正在尝试执行的 32 位整数.我会通过循环遍历十六进制字符串的字符并将每个字符转换为长度为 4 的二进制字符串(在左侧用0"填充)来解决这个问题.
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").
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
这篇关于将十六进制值字符串转换为二进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!