Junk character when using System.Text.Encoding.GetEncoding.GetString
I am ex开发者_运维百科tracting string from an byte array. The string is a sql script.
String sql = System.Text.Encoding.GetEncoding(1200).GetString(script);
The first character is coming out to be junk(square box in preview). Due to which the whole script is failing. Any idea why this is happening?
I don't want to specifically remove the first character. More interested in knowing why and how can this be avoided.
The first character(s) are probably Byte Order Marks (BOM).
You can use a StreamReader to automatically detect any BOM and select the appropriate encoding:
byte[] script;
string sql;
using (var reader = new StreamReader(new MemoryStream(script), true))
{ // ↑
sql = reader.ReadToEnd(); // detectEncodingFromByteOrderMarks
}
精彩评论