0
我想将string
转换为byte
并将其输出为string
。C#将字符串转换为字节并作为字符串输出
例如: 字符串:255
输出:0xFF
richTextBox1.AppendText(textBox1.Text + " || " + Convert.ToBytes(textBox1.Text) + "\n");
我得到System.Byte[]
,而不是价值。
我想将string
转换为byte
并将其输出为string
。C#将字符串转换为字节并作为字符串输出
例如: 字符串:255
输出:0xFF
richTextBox1.AppendText(textBox1.Text + " || " + Convert.ToBytes(textBox1.Text) + "\n");
我得到System.Byte[]
,而不是价值。
有没有实现数组来做到这一点。你必须自己写。类似下面的代码:
byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6 };
string output = string.Empty;
foreach (byte item in bytes)
{
output += Convert.ToString(item, 16).ToUpper().PadLeft(2,'0');
}
Console.WriteLine(output);
//or using string.Format
bytes = new byte[] { 1, 2, 3, 14, 15, 16 };
output = string.Empty;
foreach (byte item in bytes)
{
output = string.Format("{0},{1:X}", output, item);
}
Console.WriteLine(output);
工程就像一个魅力,谢谢 –
我想你的意思是你要完全由十进制数字字符串,表示从0到255之间,表示在十六进制(大写)值的字符串数值转换与一个“0x”前缀。 –