Write bytes to file

前端 未结 5 1025
迷失自我
迷失自我 2020-12-01 00:18

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           


        
5条回答
  •  长情又很酷
    2020-12-01 01:03

    If I understand you correctly, this should do the trick. You'll need add using System.IO at the top of your file if you don't already have it.

    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;
        }
    }
    

提交回复
热议问题