写字节文件 [英] Write bytes to file

查看:134
本文介绍了写字节文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个十六进制字符串(如 0CFE9E69271557822FE715A8B3E564BE ),我想将它写入文件的字节。例如,

I have a hexadecimal string (e.g 0CFE9E69271557822FE715A8B3E564BE) and I want to write it to a file as bytes. For example,

Offset      0  1  2  3  4  5  6  7   8  9 10 11 12 13 14 15
00000000   0C FE 9E 69 27 15 57 82  2F E7 15 A8 B3 E5 64 BE   .þži'.W‚/ç.¨³åd¾

我怎样才能做到这一点使用.NET和C#?

How can I accomplish this using .NET and C#?

推荐答案

如果我理解正确:

public bool ByteArrayToFile(string _FileName, byte[] _ByteArray)
{
   try
   {
      // Open file for reading
      System.IO.FileStream _FileStream = 
         new System.IO.FileStream(_FileName, System.IO.FileMode.Create,
                                  System.IO.FileAccess.Write);
      // Writes a block of bytes to this stream using data from
      // a byte array.
      _FileStream.Write(_ByteArray, 0, _ByteArray.Length);

      // close file stream
      _FileStream.Close();

      return true;
   }
   catch (Exception _Exception)
   {
      // Error
      Console.WriteLine("Exception caught in process: {0}",
                        _Exception.ToString());
   }

   // error occured, return false
   return false;
}

这篇关于写字节文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆