How to generate repetitive bit pattern (strings) & export into text file?

c#, string, text-files

Solution

I suspect there is some problem with the default `Encoding` used by `File.WriteAllText` method.

Try passing encoding you need explicitly and that works fine. for instance `Encoding.UTF8`.

File.WriteAllText(@"BField_pattern_01010101.txt", pattern_01010101, Encoding.UTF8);

I've investigated that `WriteAllText` also uses "UTF8Encoding" by default. but the only difference is with arguments passed in contructor. Encoding.UTF8 uses `new UTF8Encoding(true, false);` where as WriteAllText method uses `new UTF8Encoding(false, true);`

As noted in comments BOM is the one causing trouble, Thanks @BjörnRoberg. First parameter of "UTF8Encoding constructor" defines whether to emit BOM or not.

Problem

I'm trying to generate a bit pattern(repetitive strings) and export into a text file, Here's my code: ``` string pattern_01010101 = ""; for (int i = 0; i < 10; i++) { pattern_01010101 += "0,1,0,1,0,1,0,1,"; } System.IO.File.WriteAllText(@"C:\BField_pattern_01010101.txt", pattern_01010101); ``` Result: Now if I change the loop value to "20", ``` string pattern_01010101 = ""; for (int i = 0; i < 20; i++) { pattern_01010101 += "0,1,0,1,0,1,0,1,"; } System.IO.File.WriteAllText(@"C:\BField_pattern_01010101.txt", pattern_01010101); ``` Result: I get this funny little rectangle boxes, could somebody please suggest me, what am I doing wrong here?? Many thanks for your time..:)

Original source