开发者

Junk character when using System.Text.Encoding.GetEncoding.GetString

开发者 https://www.devze.com 2023-01-28 22:26 出处:网络
I am ex开发者_运维百科tracting string from an byte array. The string is a sql script. String sql = System.Text.Encoding.GetEncoding(1200).GetString(script);

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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号