From e2c272b3d5a5abde2ecee83b7b02f312e52a122f Mon Sep 17 00:00:00 2001 From: hungdk Date: Mon, 15 Sep 2025 16:14:33 +0700 Subject: [PATCH] Change encoding method --- .../Scripts/Common/ByteToStringUtils.cs | 64 +++++++++++++++---- 1 file changed, 52 insertions(+), 12 deletions(-) diff --git a/Assets/PerfectWorld/Scripts/Common/ByteToStringUtils.cs b/Assets/PerfectWorld/Scripts/Common/ByteToStringUtils.cs index 3175d971c3..755ae54c74 100644 --- a/Assets/PerfectWorld/Scripts/Common/ByteToStringUtils.cs +++ b/Assets/PerfectWorld/Scripts/Common/ByteToStringUtils.cs @@ -9,21 +9,33 @@ namespace ModelRenderer.Scripts.Common { if (ushortArray == null || ushortArray.Length == 0) return string.Empty; - - // First convert ushort array to byte array - // Each ushort (16 bits) can be up to two bytes in GBK - byte[] byteArray = new byte[ushortArray.Length * 2]; - + + // Determine actual length up to the first null terminator + int codeUnitCount = ushortArray.Length; + for (int i = 0; i < ushortArray.Length; i++) + { + if (ushortArray[i] == 0) + { + codeUnitCount = i; + break; + } + } + + if (codeUnitCount == 0) + return string.Empty; + + // Convert ushort code units to a byte array (UTF-16LE expected) + byte[] byteArray = new byte[codeUnitCount * 2]; Buffer.BlockCopy(ushortArray, 0, byteArray, 0, byteArray.Length); - - // Convert bytes to string using GBK encoding + + // Convert bytes to string using Unicode (UTF-16LE) try { return Encoding.Unicode.GetString(byteArray); } catch (Exception ex) { - UnityEngine.Debug.LogError($"Error converting bytes to GBK string: {ex.Message}"); + UnityEngine.Debug.LogError($"Error converting bytes to Unicode string: {ex.Message}"); return string.Empty; } } @@ -46,15 +58,29 @@ namespace ModelRenderer.Scripts.Common { if (byteArray == null || byteArray.Length == 0) return string.Empty; - + try { // Code page 936 is the code page for Simplified Chinese (GB2312/GBK) // You may need to import System.Text.Encoding.CodePages package for Unity/modern .NET Encoding cp936Encoding = Encoding.GetEncoding(936); - + + // Trim at first null terminator, if present + int length = byteArray.Length; + for (int i = 0; i < byteArray.Length; i++) + { + if (byteArray[i] == 0) + { + length = i; + break; + } + } + + if (length <= 0) + return string.Empty; + // Convert the byte array to a string using code page 936 encoding - return cp936Encoding.GetString(byteArray); + return cp936Encoding.GetString(byteArray, 0, length); } catch (Exception ex) { @@ -68,7 +94,21 @@ namespace ModelRenderer.Scripts.Common if (byteArray == null || byteArray.Length == 0) return string.Empty; - return Encoding.Unicode.GetString(byteArray); + // Trim at first null terminator (two-byte aligned not guaranteed; treat any 0 byte as terminator) + int length = byteArray.Length; + for (int i = 0; i < byteArray.Length; i++) + { + if (byteArray[i] == 0) + { + length = i; + break; + } + } + + if (length <= 0) + return string.Empty; + + return Encoding.Unicode.GetString(byteArray, 0, length); } } } \ No newline at end of file