0

I'm trying to write a string into a binary file, everything is correct but the 2 first chars are wrong (ù.). It goes like this :enter image description here

Her is the code of the function:

public static void WriteFile(string filePath, string content)
    {
        try
        {
            using (BinaryWriter binWriter = new BinaryWriter(File.Open(filePath, FileMode.Create)))
            {
                binWriter.Write(content);
            }
        }
        catch (Exception err)
        {
            Console.WriteLine(err);
        }
    }

Thanks for the help

Laurent K
  • 15
  • 7
  • File.WriteAllText(filePath, content); may be what you are after. Unless you really are trying to write binary data. – Anthony G. Nov 04 '22 at 20:54
  • I absolutely need a binary file :/ – Laurent K Nov 04 '22 at 21:08
  • binarywriter prefixes strings with one or more length bytes. see [here](https://stackoverflow.com/questions/16037365/c-sharp-binarywriter-length-prefix-utf7-encoding), especially useful: Mormegil's answer! Here, I believe the length is 0x616 - How will the file be consumed? - Binaryreader will expect this prefirx.. – TaW Nov 04 '22 at 21:08
  • 1
    Can you clarify what format of string data you expect in binary file? – Alexei Levenkov Nov 04 '22 at 21:24

0 Answers0