diff --git a/Apewer/TextUtility.cs b/Apewer/TextUtility.cs index fb10acb..0170d82 100644 --- a/Apewer/TextUtility.cs +++ b/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; } }