有一个Byte型数组,用什么方法把每个字节都以十六进制的形式写到一个文档中,比如data[1] = 255,写到文档中为AA。每个字节之间最好有一个空格
同样,用什么方法把这样的数据读出来。
谢谢各位大大!!!
同样,用什么方法把这样的数据读出来。
谢谢各位大大!!!
解决方案 »
- 在线等,各位大哥大姐帮帮忙,winform问题
- 如何限制DataGridView中某列只能输入汉字?
- 文档的转换
- 未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 System.Data.dll 中。
- win32 api 中有这样一个函数 MiniDumpWriteDump 在c#中如何调用?
- 如何将DataTable写入到一个Excel中
- winform中,图片右键点击如何出现菜单?
- 根据UDP格式修改代码 求救!!!
- c# 控制台 txt 输入输出流 只求关键函数
- [求助]怎么获得要画出来的文本的宽和高?
- 请问全文搜索如何搜索出没有分词出来的字?
- 用C#直接写Transaction的问题
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < source.Length; i++)
{
sb.Append(source[i].ToString("x2"));
sb.Append(" ");
}
return sb.ToString();
}
{
int length = bytContents.Length;
StringBuilder builder = new StringBuilder(length * 3);
foreach (byte value in bytContents)
{
builder.AppendFormat("{0:X} ", value);
}
System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\test.txt",false, System.Text.Encoding.Default);
sw.Write(builder.ToString());
sw.Close();
} private static byte[] Read(string strFile)
{
System.IO.StreamReader sr = new System.IO.StreamReader(strFile, System.Text.Encoding.Default);
string strContent = sr.ReadToEnd();
sr.Close();
string[] arry = strContent.Split(' ');
List<byte> lstRet=new List<byte> ();
foreach (string s in arry)
{
if (s.Length == 2)
{
lstRet.Add(Convert.ToByte(s, 16));
}
}
return lstRet.ToArray();
}
读的话Convert.ToByte(“0xff”,16);
string s = string.Join(" ", b.Select(x => ((int)x).ToString("X").PadLeft(2, '0')).ToArray()); // FF AA
byte[] buffer = new byte[] { 255, 120, 99, 15 };
Console.WriteLine(string.Join("-", buffer.Select(item => item.ToString("x2"))));
这个是返回去的