问题描述
嗨
如果我有一个byte []数组,并且想在类似0x3130353030330353032的文本框中显示它,该怎么办?我知道如何获得它的实际值"10500502".但是我想知道如何获取C#byte []对象并将其显示为0x3130353030353053032,基本上与SQL在查询窗口中显示的方式相同.
谢谢
Hi
If I have a byte[] array and I want to display it in a textbox like 0x3130353030353032, how would I do this? I know how to get it''s actual value which is "10500502". but I want to know how to take the C# byte[] object and display it as 0x3130353030353032 basically the same way that SQL does it in a query window.
Thanks
推荐答案
myTextBox.Text="0x";
foreach(byte b in array)
{
myTextBox.Text += b.ToString("X2");
}
如果数组相对较长,则可以考虑使用 StringBuilder :: Append方法 [ ^ ],而不是String
''+=''
运算符以获得更好的性能.
If you array is relatively long, you may consider using StringBuilder::Append method[^], instead of the String
''+=''
operator for better performance.
class Program
{
static void Main(string[] args)
{
byte[] myBytes = Encoding.ASCII.GetBytes("Hello World");
Console.WriteLine(GetString(myBytes));
}
private static String GetString(byte[] myBytes)
{
StringBuilder builder = new StringBuilder();
builder.Append("0x");
Array.ForEach(myBytes, item => builder.Append(item.ToString()));
return builder.ToString();
}
}
:)
:)
这篇关于将byte []显示为纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!