using System; using System.Text; namespace ModelRenderer.Scripts.Common { public class ByteToStringUtils { public static string UshortArrayToUnicodeString(ushort[] ushortArray) { 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]; Buffer.BlockCopy(ushortArray, 0, byteArray, 0, byteArray.Length); // Convert bytes to string using GBK encoding try { return Encoding.Unicode.GetString(byteArray); } catch (Exception ex) { UnityEngine.Debug.LogError($"Error converting bytes to GBK string: {ex.Message}"); return string.Empty; } } public static string UshortArrayToCP936String(ushort[] ushortArray) { 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]; Buffer.BlockCopy(ushortArray, 0, byteArray, 0, byteArray.Length); return ByteArrayToCP936String(byteArray); } public static string ByteArrayToCP936String(byte[] byteArray) { 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); // Convert the byte array to a string using code page 936 encoding return cp936Encoding.GetString(byteArray); } catch (Exception ex) { UnityEngine.Debug.LogError($"Error converting bytes to CP936 string: {ex.Message}"); return string.Empty; } } public static string ByteArrayToUnicodeString(byte[] byteArray) { if (byteArray == null || byteArray.Length == 0) return string.Empty; return Encoding.Unicode.GetString(byteArray); } } }