From 9ee2b88b23b9929fed73be374781c99ff5dc2ff9 Mon Sep 17 00:00:00 2001 From: Elivo Date: Thu, 28 Aug 2025 17:23:13 +0800 Subject: [PATCH] =?UTF-8?q?FromHex=EF=BC=8C=E4=BC=98=E5=8C=96=E6=80=A7?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer/BytesUtility.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Apewer/BytesUtility.cs b/Apewer/BytesUtility.cs index 0f983ce..561608d 100644 --- a/Apewer/BytesUtility.cs +++ b/Apewer/BytesUtility.cs @@ -186,19 +186,18 @@ namespace Apewer public static byte[] FromHex(this string hex) { if (string.IsNullOrEmpty(hex)) return Empty; - hex = hex.Replace(" ", "").Replace("-", ""); - if (string.IsNullOrEmpty(hex) || hex.Length < 2) return Empty; - if (hex.Length % 2 != 0) return Empty; + var chars = hex.ToLower().ToCharArray(); + chars = chars.FindAll(x => x != ' ' && x != '-'); + if (chars.Length < 1 || chars.Length % 2 != 0) return Empty; - var lower = hex.ToLower().ToCharArray(); - var half = lower.Length / 2; + var half = chars.Length / 2; var bytes = new byte[half]; for (var i = 0; i < half; i++) { var offset = i * 2; - var h = Constant.HexCollection.IndexOf(lower[offset]); - var l = Constant.HexCollection.IndexOf(lower[offset + 1]); + var h = Constant.HexCollection.IndexOf(chars[offset]); + var l = Constant.HexCollection.IndexOf(chars[offset + 1]); if (h < 0 || l < 0) return Empty; bytes[i] = Convert.ToByte((h * 16) + l); }