114 lines
3.8 KiB
C#
114 lines
3.8 KiB
C#
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;
|
|
|
|
// 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 Unicode (UTF-16LE)
|
|
try
|
|
{
|
|
return Encoding.Unicode.GetString(byteArray);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
UnityEngine.Debug.LogError($"Error converting bytes to Unicode 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);
|
|
|
|
// 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, 0, length);
|
|
}
|
|
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;
|
|
|
|
// 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);
|
|
}
|
|
}
|
|
} |