显示纯文本byte []作为byte [] [英] display plain text byte[] as byte[]
本文介绍了显示纯文本byte []作为byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
不久前,我问了一个有关如何将byte []转换为看起来像0x3130353030330353032的字符串的问题.
解决方案是
Hi
I asked the question a while back on how to convert a byte[] to a string looking something like 0x3130353030353032.
The solution was
myTextBox.Text="0x";
foreach(byte b in array)
{
myTextBox.Text += b.ToString("X2");
}
现在我想知道如何将字符串转换回字节[]
谢谢Advanced
now I would please like to know how would I convert that string back to a byte[]
Thanks in advanced
推荐答案
两个提示:
1)如您所知,一个字节使用两个十六进制数字表示为十六进制字符串.将您的字符串拆分为两位数的字符串数组;此数组的长度将为您提供应初始化的byte[]
数组的长度.
2)要将字符串解析为字节,请使用byte.Parse(string, NumberStyles)
和System.Globalization.NumberStyles.HexNumber
.
参见:
http://msdn.microsoft.com/en-us/library/4eszwye3.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.globalization. numberstyles.aspx [ ^ ].
—SA
Two hints:
1) As you should know, a byte takes two hexadecimal digits to be presented as a hexadecimal string. Split your string into the array of two-digit strings; the length of this array will give you the length of thebyte[]
array you should initialize.
2) To parse a string into byte, usebyte.Parse(string, NumberStyles)
, useSystem.Globalization.NumberStyles.HexNumber
.
See:
http://msdn.microsoft.com/en-us/library/4eszwye3.aspx[^],
http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles.aspx[^].
—SA
这篇关于显示纯文本byte []作为byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文