From e8b294cf81b084eae7979fa5181607c757cbbaa0 Mon Sep 17 00:00:00 2001 From: Elivo Date: Fri, 18 Jul 2025 16:01:32 +0800 Subject: [PATCH] =?UTF-8?q?TextUtility.FromBytes=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E9=9B=B6=E5=80=BC=E6=A3=80=E6=9F=A5=EF=BC=8C=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=E6=88=AA=E6=96=AD=E5=AD=97=E7=AC=A6=E4=B8=B2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer/TextUtility.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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; } }