Posts

Showing posts with the label filestream

c# - write string that contains hex values as bytes

c# - write string that contains hex values as bytes let's say I have a string string hex = "55 6E 6B 6E 6F 77 6E 20 53 70 61 63 65" I want to write these to a file but they should be the bytes itself. so the result would be Unknown Space. Is there any way to do that directly instead of first encoding it to utf-8 and write that to the file? the reason I'm asking is that sometimes there is the special character of utf-8 in the (like e.g. 0xE28780 ) which wouldn't work well if I first split the string into chars of each value. encoding utf-8 utf-8 0xE28780 Thanks a lot! 1 Answer 1 with string.Split , Convert.ToByte , and FileStream or File.WriteAllBytes string.Split Convert.ToByte FileStream File.WriteAllBytes string hex = "55 6E 6B 6E 6F 77 6E 20 53 70 61 63 65"; var bytes = hex.Split(' ') .Select(x => Convert.ToByte(x, 16)) ...