298 lines
8.0 KiB
C#
298 lines
8.0 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
public class DBitConverter
|
|
{
|
|
|
|
public static byte[] GetBytes(float s, bool asc)
|
|
{
|
|
int buf = (int)(s * 100);
|
|
return GetBytes(buf, asc);
|
|
}
|
|
|
|
public static byte[] GetBytes(short s, bool asc)
|
|
{
|
|
byte[] buf = new byte[2];
|
|
if (asc)
|
|
{
|
|
// 大端序(高位在前)
|
|
for (int i = buf.Length - 1; i >= 0; i--)
|
|
{
|
|
buf[i] = (byte)(s & 0x00ff);
|
|
s >>= 8;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 小端序(低位在前)
|
|
for (int i = 0; i < buf.Length; i++)
|
|
{
|
|
|
|
buf[i] = (byte)(s & 0x00ff);
|
|
s >>= 8;
|
|
}
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
public static byte[] GetBytes(ushort s, bool asc)
|
|
{
|
|
byte[] buf = new byte[2];
|
|
if (asc)
|
|
{
|
|
// 大端序(高位在前)
|
|
for (int i = buf.Length - 1; i >= 0; i--)
|
|
{
|
|
buf[i] = (byte)(s & 0x00ff); // 取最低 8 位
|
|
s >>= 8; // 右移 8 位
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 小端序(低位在前)
|
|
for (int i = 0; i < buf.Length; i++)
|
|
{
|
|
buf[i] = (byte)(s & 0x00ff); // 取最低 8 位
|
|
s >>= 8; // 右移 8 位
|
|
}
|
|
}
|
|
return buf;
|
|
}
|
|
public static byte[] GetBytes(int s, bool asc)
|
|
{
|
|
byte[] buf = new byte[4];
|
|
if (asc)
|
|
for (int i = buf.Length - 1; i >= 0; i--)
|
|
{
|
|
buf[i] = (byte)(s & 0x000000ff);
|
|
s >>= 8;
|
|
}
|
|
else
|
|
for (int i = 0; i < buf.Length; i++)
|
|
{
|
|
buf[i] = (byte)(s & 0x000000ff);
|
|
s >>= 8;
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
public static byte[] GetBytes(uint s, bool asc)
|
|
{
|
|
byte[] buf = new byte[4];
|
|
if (asc)
|
|
{
|
|
// 大端序(高位在前)
|
|
for (int i = buf.Length - 1; i >= 0; i--)
|
|
{
|
|
buf[i] = (byte)(s & 0x000000ff); // 取最低 8 位
|
|
s >>= 8; // 右移 8 位
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 小端序(低位在前)
|
|
for (int i = 0; i < buf.Length; i++)
|
|
{
|
|
buf[i] = (byte)(s & 0x000000ff); // 取最低 8 位
|
|
s >>= 8; // 右移 8 位
|
|
}
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
public static byte[] GetBytes(long s, bool asc)
|
|
{
|
|
byte[] buf = new byte[8];
|
|
if (asc)
|
|
for (int i = buf.Length - 1; i >= 0; i--)
|
|
{
|
|
buf[i] = (byte)(s & 0x00000000000000ff);
|
|
s >>= 8;
|
|
}
|
|
else
|
|
for (int i = 0; i < buf.Length; i++)
|
|
{
|
|
buf[i] = (byte)(s & 0x00000000000000ff);
|
|
s >>= 8;
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
public static byte[] GetBytes(byte b)
|
|
{
|
|
byte[] buf = new byte[1];
|
|
buf[0] = b;
|
|
return buf;
|
|
}
|
|
|
|
public static byte[] GetBytesInt(int s, bool asc)
|
|
{
|
|
byte[] buf = new byte[4];
|
|
if (asc)
|
|
for (int i = buf.Length - 1; i >= 0; i--)
|
|
{
|
|
buf[i] = (byte)(s & 0x000000ff);
|
|
s >>= 8;
|
|
}
|
|
else
|
|
for (int i = 0; i < buf.Length; i++)
|
|
{
|
|
buf[i] = (byte)(s & 0x000000ff);
|
|
s >>= 8;
|
|
}
|
|
return buf;
|
|
}
|
|
public static float ToFloat(byte[] buf, int startIndex, bool asc)
|
|
{
|
|
int i = ToInt(buf, startIndex, asc);
|
|
float s = (float)i;
|
|
return s / 100;
|
|
}
|
|
|
|
|
|
public static short ToShort(byte[] buffer, int startIndex, bool asc)
|
|
{
|
|
// 检查输入参数的有效性
|
|
if (buffer == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(buffer), "输入的字节数组不能为 null。");
|
|
}
|
|
if (startIndex < 0 || startIndex > buffer.Length - 2)
|
|
{
|
|
throw new ArgumentOutOfRangeException(nameof(startIndex), "起始索引超出了有效范围。");
|
|
}
|
|
// 获取两个字节
|
|
byte byte1 = buffer[startIndex];
|
|
byte byte2 = buffer[startIndex + 1];
|
|
if (asc) // 大端序
|
|
{
|
|
// 大端序高位字节在前,低位字节在后
|
|
return (short)((byte1 << 8) | byte2);
|
|
}
|
|
else // 小端序
|
|
{
|
|
// 小端序低位字节在前,高位字节在后
|
|
return (short)((byte2 << 8) | byte1);
|
|
}
|
|
}
|
|
public static ushort ToUshort(byte[] buffer, int startIndex, bool asc)
|
|
{
|
|
if (buffer == null)
|
|
{
|
|
Debug.LogError("Buffer Is Null !");
|
|
return 0; // 返回默认值
|
|
}
|
|
if (buffer.Length < startIndex + 2)
|
|
{
|
|
Debug.LogError("Invalid Length , Current Length:" + buffer.Length + " , Need Length : " + (startIndex + 2));
|
|
return 0; // 返回默认值
|
|
}
|
|
|
|
ushort r = 0;
|
|
if (!asc)
|
|
{
|
|
// 小端序:从低位字节开始处理
|
|
for (int i = startIndex; i < startIndex + 2; i++)
|
|
{
|
|
r |= (ushort)((buffer[i] & 0x00FF) << (8 * (i - startIndex)));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 大端序:从高位字节开始处理
|
|
for (int i = startIndex; i < startIndex + 2; i++)
|
|
{
|
|
r |= (ushort)((buffer[i] & 0x00FF) << (8 * (1 - (i - startIndex))));
|
|
}
|
|
}
|
|
|
|
return r;
|
|
}
|
|
public static int ToInt(byte[] buffer, int startIndex, bool asc)
|
|
{
|
|
if (buffer == null)
|
|
{
|
|
Debug.LogError("Buffer Is Null !");
|
|
}
|
|
if (buffer.Length < startIndex + 4)
|
|
{
|
|
Debug.LogError("Invalid Length , Current Length:" + buffer.Length + " , Need Length : " + (startIndex + 4));
|
|
}
|
|
int r = 0;
|
|
if (!asc)
|
|
for (int i = buffer.Length - 1 - 4; i >= startIndex; i--)
|
|
{
|
|
r <<= 8;
|
|
r |= (buffer[i] & 0x000000ff);
|
|
}
|
|
else
|
|
for (int i = startIndex; i < startIndex + 4; i++)
|
|
{
|
|
r <<= 8;
|
|
r |= (buffer[i] & 0x000000ff);
|
|
}
|
|
return r;
|
|
}
|
|
public static uint ToUint(byte[] buffer, int startIndex, bool asc)
|
|
{
|
|
if (buffer == null)
|
|
{
|
|
Debug.LogError("Buffer Is Null !");
|
|
return 0; // 返回默认值
|
|
}
|
|
if (buffer.Length < startIndex + 4)
|
|
{
|
|
Debug.LogError("Invalid Length , Current Length:" + buffer.Length + " , Need Length : " + (startIndex + 4));
|
|
return 0; // 返回默认值
|
|
}
|
|
|
|
uint r = 0;
|
|
if (!asc)
|
|
{
|
|
for (int i = buffer.Length - 1 - 4; i >= startIndex; i--)
|
|
{
|
|
r <<= 8;
|
|
r |= (uint)(buffer[i] & 0x000000ff);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int i = startIndex; i < startIndex + 4; i++)
|
|
{
|
|
r <<= 8;
|
|
r |= (uint)(buffer[i] & 0x000000ff);
|
|
}
|
|
}
|
|
|
|
return r;
|
|
}
|
|
public static long ToLong(byte[] buffer, int startIndex, bool asc)
|
|
{
|
|
if (buffer == null)
|
|
{
|
|
Debug.LogError("Buffer Is Null !");
|
|
}
|
|
if (buffer.Length < startIndex + 8)
|
|
{
|
|
Debug.LogError("Invalid Length , Current Length:" + buffer.Length + " , Need Length : " + (startIndex + 8));
|
|
}
|
|
long r = 0;
|
|
if (!asc)
|
|
for (int i = buffer.Length - 1 - 8; i >= startIndex; i--)
|
|
{
|
|
r <<= 8;
|
|
r |= (buffer[i] & 0x00000000000000ff);
|
|
}
|
|
else
|
|
for (int i = startIndex; i < startIndex + 8; i++)
|
|
{
|
|
r <<= 8;
|
|
r |= (buffer[i] & 0x00000000000000ff);
|
|
}
|
|
return r;
|
|
}
|
|
|
|
|
|
|
|
}
|