String Hex to byte, vb.net

半腔热情 提交于 2020-01-26 04:04:07

问题


I'm struggling with an easy task. At least it looks like it should be, at first sight. I have a TextBox that contains HEX strings. They are always two hex digits in length (e.g. AA). I want to convert textbox3.Text to a Byte.

Here's what I have so far:

Dim checking As String = textbox3.Text
Dim a = Convert.ToByte(checking)
RichTextBox1.Text = a.ToString 

But it throws a SystemFormatException.


回答1:


The Convert.ToByte method provides an overload which takes a string argument followed by a number specifying the base of the value in the string. Hexadecimal is base-16. So, for instance:

Dim checking As String = textbox3.Text
Dim a As Byte = Convert.ToByte(checking, 16)
RichTextBox1.Text = a.ToString()


来源:https://stackoverflow.com/questions/48016764/string-hex-to-byte-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!