Write bytes to file

前端 未结 5 1027
迷失自我
迷失自我 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:25

    You convert the hex string to a byte array.

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

    Credit: Jared Par

    And then use WriteAllBytes to write to the file system.

提交回复
热议问题