Is there a way to remove the BOM from a UTF-8 encoded file?

With ruby >= 1.9.2 you can use the mode r:bom|utf-8 This should work (I haven’t test it in combination with json): json = nil #define the variable outside the block to keep the data File.open(‘file.txt’, “r:bom|utf-8”){|file| json = JSON.parse(file.read) } It doesn’t matter, if the BOM is available in the file or not. Andrew remarked, … Read more

How to avoid tripping over UTF-8 BOM when reading files

With ruby 1.9.2 you can use the mode r:bom|utf-8 text_without_bom = nil #define the variable outside the block to keep the data File.open(‘file.txt’, “r:bom|utf-8”){|file| text_without_bom = file.read } or text_without_bom = File.read(‘file.txt’, encoding: ‘bom|utf-8’) or text_without_bom = File.read(‘file.txt’, mode: ‘r:bom|utf-8′) It doesn’t matter, if the BOM is available in the file or not. You may … Read more

XDocument: saving XML to file without BOM

Use an XmlTextWriter and pass that to the XDocument’s Save() method, that way you can have more control over the type of encoding used: var doc = new XDocument( new XDeclaration(“1.0”, “utf-8”, null), new XElement(“root”, new XAttribute(“note”, “boogers”)) ); using (var writer = new XmlTextWriter(“.\\boogers.xml”, new UTF8Encoding(false))) { doc.Save(writer); } The UTF8Encoding class constructor has … Read more

How to GetBytes() in C# with UTF8 encoding with BOM?

Try like this: public ActionResult Download() { var data = Encoding.UTF8.GetBytes(“some data”); var result = Encoding.UTF8.GetPreamble().Concat(data).ToArray(); return File(result, “application/csv”, “foo.csv”); } The reason is that the UTF8Encoding constructor that takes a boolean parameter doesn’t do what you would expect: byte[] bytes = new UTF8Encoding(true).GetBytes(“a”); The resulting array would contain a single byte with the value … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)