小智 151
如果我理解正确,这应该可以解决问题.using System.IO如果您还没有添加,则需要在文件顶部添加.
public bool ByteArrayToFile(string fileName, byte[] byteArray)
{
try
{
using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
fs.Write(byteArray, 0, byteArray.Length);
return true;
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in process: {0}", ex);
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
Don*_*nut 72
最简单的方法是将十六进制字符串转换为字节数组并使用该File.WriteAllBytes方法.
使用这个问题的StringToByteArray()方法,你会做这样的事情:
string hexString = "0CFE9E69271557822FE715A8B3E564BE";
File.WriteAllBytes("output.dat", StringToByteArray(hexString));
Run Code Online (Sandbox Code Playgroud)
该StringToByteArray方法包括在下面:
public static byte[] StringToByteArray(string hex) {
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
Run Code Online (Sandbox Code Playgroud)