Browse Source

TextUtility.FromBytes 增加零值检查,用于截断字符串。

master
王厅 1 week ago
parent
commit
e8b294cf81
  1. 18
      Apewer/TextUtility.cs

18
Apewer/TextUtility.cs

@ -234,7 +234,23 @@ namespace Apewer
public static string FromBytes(byte[] bytes, Encoding encoding = null)
{
if (bytes == null || bytes.LongLength < 1L) return Empty;
try { return (encoding ?? Encoding.UTF8).GetString(bytes); }
// 计算非零字节的长度
var count = 0;
for (var i = 0; i < bytes.Length; i++)
{
if (bytes[i] == 0) continue;
count++;
}
if (count < 1) return Empty;
// 转换
if (encoding == null) encoding = Encoding.UTF8;
try
{
var result = encoding.GetString(bytes, 0, count);
return result;
}
catch { return Empty; }
}

Loading…
Cancel
Save