//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 loyalsoft. All rights reserved.
// Homepage: http://www.game7000.com/
// Feedback: http://www.game7000.com/
//------------------------------------------------------------
using System;
using System.Text;
namespace GameFramework
{
public static partial class Utility
{
///
/// 类型转换相关的实用函数。
///
public static class Converter
{
private const float InchesToCentimeters = 2.54f; // 1 inch = 2.54 cm
private const float CentimetersToInches = 1f / InchesToCentimeters; // 1 cm = 0.3937 inches
///
/// 获取数据在此计算机结构中存储时的字节顺序。
///
public static bool IsLittleEndian
{
get
{
return BitConverter.IsLittleEndian;
}
}
///
/// 获取或设置屏幕每英寸点数。
///
public static float ScreenDpi
{
get;
set;
}
///
/// 将像素转换为厘米。
///
/// 像素。
/// 厘米。
public static float GetCentimetersFromPixels(float pixels)
{
if (ScreenDpi <= 0)
{
throw new GameFrameworkException("You must set screen DPI first.");
}
return InchesToCentimeters * pixels / ScreenDpi;
}
///
/// 将厘米转换为像素。
///
/// 厘米。
/// 像素。
public static float GetPixelsFromCentimeters(float centimeters)
{
if (ScreenDpi <= 0)
{
throw new GameFrameworkException("You must set screen DPI first.");
}
return CentimetersToInches * centimeters * ScreenDpi;
}
///
/// 将像素转换为英寸。
///
/// 像素。
/// 英寸。
public static float GetInchesFromPixels(float pixels)
{
if (ScreenDpi <= 0)
{
throw new GameFrameworkException("You must set screen DPI first.");
}
return pixels / ScreenDpi;
}
///
/// 将英寸转换为像素。
///
/// 英寸。
/// 像素。
public static float GetPixelsFromInches(float inches)
{
if (ScreenDpi <= 0)
{
throw new GameFrameworkException("You must set screen DPI first.");
}
return inches * ScreenDpi;
}
///
/// 以字节数组的形式获取指定的布尔值。
///
/// 要转换的布尔值。
/// 用于存放结果的字节数组。
public static byte[] GetBytes(bool value)
{
byte[] buffer = new byte[1];
GetBytes(value, buffer, 0);
return buffer;
}
///
/// 以字节数组的形式获取指定的布尔值。
///
/// 要转换的布尔值。
/// 用于存放结果的字节数组。
public static void GetBytes(bool value, byte[] buffer)
{
GetBytes(value, buffer, 0);
}
///
/// 以字节数组的形式获取指定的布尔值。
///
/// 要转换的布尔值。
/// 用于存放结果的字节数组。
/// buffer 内的起始位置。
public static void GetBytes(bool value, byte[] buffer, int startIndex)
{
if (buffer == null)
{
throw new GameFrameworkException("Buffer is invalid.");
}
if (startIndex < 0 || startIndex + 1 > buffer.Length)
{
throw new GameFrameworkException("Start index is invalid.");
}
buffer[startIndex] = value ? (byte)1 : (byte)0;
}
///
/// 返回由字节数组中首字节转换来的布尔值。
///
/// 字节数组。
/// 如果 value 中的首字节非零,则为 true,否则为 false。
public static bool GetBoolean(byte[] value)
{
return BitConverter.ToBoolean(value, 0);
}
///
/// 返回由字节数组中指定位置的一个字节转换来的布尔值。
///
/// 字节数组。
/// value 内的起始位置。
/// 如果 value 中指定位置的字节非零,则为 true,否则为 false。
public static bool GetBoolean(byte[] value, int startIndex)
{
return BitConverter.ToBoolean(value, startIndex);
}
///
/// 以字节数组的形式获取指定的 Unicode 字符值。
///
/// 要转换的字符。
/// 用于存放结果的字节数组。
public static byte[] GetBytes(char value)
{
byte[] buffer = new byte[2];
GetBytes((short)value, buffer, 0);
return buffer;
}
///
/// 以字节数组的形式获取指定的 Unicode 字符值。
///
/// 要转换的字符。
/// 用于存放结果的字节数组。
public static void GetBytes(char value, byte[] buffer)
{
GetBytes((short)value, buffer, 0);
}
///
/// 以字节数组的形式获取指定的 Unicode 字符值。
///
/// 要转换的字符。
/// 用于存放结果的字节数组。
/// buffer 内的起始位置。
public static void GetBytes(char value, byte[] buffer, int startIndex)
{
GetBytes((short)value, buffer, startIndex);
}
///
/// 返回由字节数组中前两个字节转换来的 Unicode 字符。
///
/// 字节数组。
/// 由两个字节构成的字符。
public static char GetChar(byte[] value)
{
return BitConverter.ToChar(value, 0);
}
///
/// 返回由字节数组中指定位置的两个字节转换来的 Unicode 字符。
///
/// 字节数组。
/// value 内的起始位置。
/// 由两个字节构成的字符。
public static char GetChar(byte[] value, int startIndex)
{
return BitConverter.ToChar(value, startIndex);
}
///
/// 以字节数组的形式获取指定的 16 位有符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static byte[] GetBytes(short value)
{
byte[] buffer = new byte[2];
GetBytes(value, buffer, 0);
return buffer;
}
///
/// 以字节数组的形式获取指定的 16 位有符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static void GetBytes(short value, byte[] buffer)
{
GetBytes(value, buffer, 0);
}
///
/// 以字节数组的形式获取指定的 16 位有符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
/// buffer 内的起始位置。
public static unsafe void GetBytes(short value, byte[] buffer, int startIndex)
{
if (buffer == null)
{
throw new GameFrameworkException("Buffer is invalid.");
}
if (startIndex < 0 || startIndex + 2 > buffer.Length)
{
throw new GameFrameworkException("Start index is invalid.");
}
fixed (byte* valueRef = buffer)
{
*(short*)(valueRef + startIndex) = value;
}
}
///
/// 返回由字节数组中前两个字节转换来的 16 位有符号整数。
///
/// 字节数组。
/// 由两个字节构成的 16 位有符号整数。
public static short GetInt16(byte[] value)
{
return BitConverter.ToInt16(value, 0);
}
///
/// 返回由字节数组中指定位置的两个字节转换来的 16 位有符号整数。
///
/// 字节数组。
/// value 内的起始位置。
/// 由两个字节构成的 16 位有符号整数。
public static short GetInt16(byte[] value, int startIndex)
{
return BitConverter.ToInt16(value, startIndex);
}
///
/// 以字节数组的形式获取指定的 16 位无符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static byte[] GetBytes(ushort value)
{
byte[] buffer = new byte[2];
GetBytes((short)value, buffer, 0);
return buffer;
}
///
/// 以字节数组的形式获取指定的 16 位无符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static void GetBytes(ushort value, byte[] buffer)
{
GetBytes((short)value, buffer, 0);
}
///
/// 以字节数组的形式获取指定的 16 位无符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
/// buffer 内的起始位置。
public static void GetBytes(ushort value, byte[] buffer, int startIndex)
{
GetBytes((short)value, buffer, startIndex);
}
///
/// 返回由字节数组中前两个字节转换来的 16 位无符号整数。
///
/// 字节数组。
/// 由两个字节构成的 16 位无符号整数。
public static ushort GetUInt16(byte[] value)
{
return BitConverter.ToUInt16(value, 0);
}
///
/// 返回由字节数组中指定位置的两个字节转换来的 16 位无符号整数。
///
/// 字节数组。
/// value 内的起始位置。
/// 由两个字节构成的 16 位无符号整数。
public static ushort GetUInt16(byte[] value, int startIndex)
{
return BitConverter.ToUInt16(value, startIndex);
}
///
/// 以字节数组的形式获取指定的 32 位有符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static byte[] GetBytes(int value)
{
byte[] buffer = new byte[4];
GetBytes(value, buffer, 0);
return buffer;
}
///
/// 以字节数组的形式获取指定的 32 位有符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static void GetBytes(int value, byte[] buffer)
{
GetBytes(value, buffer, 0);
}
///
/// 以字节数组的形式获取指定的 32 位有符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
/// buffer 内的起始位置。
public static unsafe void GetBytes(int value, byte[] buffer, int startIndex)
{
if (buffer == null)
{
throw new GameFrameworkException("Buffer is invalid.");
}
if (startIndex < 0 || startIndex + 4 > buffer.Length)
{
throw new GameFrameworkException("Start index is invalid.");
}
fixed (byte* valueRef = buffer)
{
*(int*)(valueRef + startIndex) = value;
}
}
///
/// 返回由字节数组中前四个字节转换来的 32 位有符号整数。
///
/// 字节数组。
/// 由四个字节构成的 32 位有符号整数。
public static int GetInt32(byte[] value)
{
return BitConverter.ToInt32(value, 0);
}
///
/// 返回由字节数组中指定位置的四个字节转换来的 32 位有符号整数。
///
/// 字节数组。
/// value 内的起始位置。
/// 由四个字节构成的 32 位有符号整数。
public static int GetInt32(byte[] value, int startIndex)
{
return BitConverter.ToInt32(value, startIndex);
}
///
/// 以字节数组的形式获取指定的 32 位无符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static byte[] GetBytes(uint value)
{
byte[] buffer = new byte[4];
GetBytes((int)value, buffer, 0);
return buffer;
}
///
/// 以字节数组的形式获取指定的 32 位无符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static void GetBytes(uint value, byte[] buffer)
{
GetBytes((int)value, buffer, 0);
}
///
/// 以字节数组的形式获取指定的 32 位无符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
/// buffer 内的起始位置。
public static void GetBytes(uint value, byte[] buffer, int startIndex)
{
GetBytes((int)value, buffer, startIndex);
}
///
/// 返回由字节数组中前四个字节转换来的 32 位无符号整数。
///
/// 字节数组。
/// 由四个字节构成的 32 位无符号整数。
public static uint GetUInt32(byte[] value)
{
return BitConverter.ToUInt32(value, 0);
}
///
/// 返回由字节数组中指定位置的四个字节转换来的 32 位无符号整数。
///
/// 字节数组。
/// value 内的起始位置。
/// 由四个字节构成的 32 位无符号整数。
public static uint GetUInt32(byte[] value, int startIndex)
{
return BitConverter.ToUInt32(value, startIndex);
}
///
/// 以字节数组的形式获取指定的 64 位有符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static byte[] GetBytes(long value)
{
byte[] buffer = new byte[8];
GetBytes(value, buffer, 0);
return buffer;
}
///
/// 以字节数组的形式获取指定的 64 位有符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static void GetBytes(long value, byte[] buffer)
{
GetBytes(value, buffer, 0);
}
///
/// 以字节数组的形式获取指定的 64 位有符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
/// buffer 内的起始位置。
public static unsafe void GetBytes(long value, byte[] buffer, int startIndex)
{
if (buffer == null)
{
throw new GameFrameworkException("Buffer is invalid.");
}
if (startIndex < 0 || startIndex + 8 > buffer.Length)
{
throw new GameFrameworkException("Start index is invalid.");
}
fixed (byte* valueRef = buffer)
{
*(long*)(valueRef + startIndex) = value;
}
}
///
/// 返回由字节数组中前八个字节转换来的 64 位有符号整数。
///
/// 字节数组。
/// 由八个字节构成的 64 位有符号整数。
public static long GetInt64(byte[] value)
{
return BitConverter.ToInt64(value, 0);
}
///
/// 返回由字节数组中指定位置的八个字节转换来的 64 位有符号整数。
///
/// 字节数组。
/// value 内的起始位置。
/// 由八个字节构成的 64 位有符号整数。
public static long GetInt64(byte[] value, int startIndex)
{
return BitConverter.ToInt64(value, startIndex);
}
///
/// 以字节数组的形式获取指定的 64 位无符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static byte[] GetBytes(ulong value)
{
byte[] buffer = new byte[8];
GetBytes((long)value, buffer, 0);
return buffer;
}
///
/// 以字节数组的形式获取指定的 64 位无符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static void GetBytes(ulong value, byte[] buffer)
{
GetBytes((long)value, buffer, 0);
}
///
/// 以字节数组的形式获取指定的 64 位无符号整数值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
/// buffer 内的起始位置。
public static void GetBytes(ulong value, byte[] buffer, int startIndex)
{
GetBytes((long)value, buffer, startIndex);
}
///
/// 返回由字节数组中前八个字节转换来的 64 位无符号整数。
///
/// 字节数组。
/// 由八个字节构成的 64 位无符号整数。
public static ulong GetUInt64(byte[] value)
{
return BitConverter.ToUInt64(value, 0);
}
///
/// 返回由字节数组中指定位置的八个字节转换来的 64 位无符号整数。
///
/// 字节数组。
/// value 内的起始位置。
/// 由八个字节构成的 64 位无符号整数。
public static ulong GetUInt64(byte[] value, int startIndex)
{
return BitConverter.ToUInt64(value, startIndex);
}
///
/// 以字节数组的形式获取指定的单精度浮点值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static unsafe byte[] GetBytes(float value)
{
byte[] buffer = new byte[4];
GetBytes(*(int*)&value, buffer, 0);
return buffer;
}
///
/// 以字节数组的形式获取指定的单精度浮点值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static unsafe void GetBytes(float value, byte[] buffer)
{
GetBytes(*(int*)&value, buffer, 0);
}
///
/// 以字节数组的形式获取指定的单精度浮点值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
/// buffer 内的起始位置。
public static unsafe void GetBytes(float value, byte[] buffer, int startIndex)
{
GetBytes(*(int*)&value, buffer, startIndex);
}
///
/// 返回由字节数组中前四个字节转换来的单精度浮点数。
///
/// 字节数组。
/// 由四个字节构成的单精度浮点数。
public static float GetSingle(byte[] value)
{
return BitConverter.ToSingle(value, 0);
}
///
/// 返回由字节数组中指定位置的四个字节转换来的单精度浮点数。
///
/// 字节数组。
/// value 内的起始位置。
/// 由四个字节构成的单精度浮点数。
public static float GetSingle(byte[] value, int startIndex)
{
return BitConverter.ToSingle(value, startIndex);
}
///
/// 以字节数组的形式获取指定的双精度浮点值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static unsafe byte[] GetBytes(double value)
{
byte[] buffer = new byte[8];
GetBytes(*(long*)&value, buffer, 0);
return buffer;
}
///
/// 以字节数组的形式获取指定的双精度浮点值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
public static unsafe void GetBytes(double value, byte[] buffer)
{
GetBytes(*(long*)&value, buffer, 0);
}
///
/// 以字节数组的形式获取指定的双精度浮点值。
///
/// 要转换的数字。
/// 用于存放结果的字节数组。
/// buffer 内的起始位置。
public static unsafe void GetBytes(double value, byte[] buffer, int startIndex)
{
GetBytes(*(long*)&value, buffer, startIndex);
}
///
/// 返回由字节数组中前八个字节转换来的双精度浮点数。
///
/// 字节数组。
/// 由八个字节构成的双精度浮点数。
public static double GetDouble(byte[] value)
{
return BitConverter.ToDouble(value, 0);
}
///
/// 返回由字节数组中指定位置的八个字节转换来的双精度浮点数。
///
/// 字节数组。
/// value 内的起始位置。
/// 由八个字节构成的双精度浮点数。
public static double GetDouble(byte[] value, int startIndex)
{
return BitConverter.ToDouble(value, startIndex);
}
///
/// 以字节数组的形式获取 UTF-8 编码的字符串。
///
/// 要转换的字符串。
/// 用于存放结果的字节数组。
public static byte[] GetBytes(string value)
{
return GetBytes(value, Encoding.UTF8);
}
///
/// 以字节数组的形式获取 UTF-8 编码的字符串。
///
/// 要转换的字符串。
/// 用于存放结果的字节数组。
/// buffer 内实际填充了多少字节。
public static int GetBytes(string value, byte[] buffer)
{
return GetBytes(value, Encoding.UTF8, buffer, 0);
}
///
/// 以字节数组的形式获取 UTF-8 编码的字符串。
///
/// 要转换的字符串。
/// 用于存放结果的字节数组。
/// buffer 内的起始位置。
/// buffer 内实际填充了多少字节。
public static int GetBytes(string value, byte[] buffer, int startIndex)
{
return GetBytes(value, Encoding.UTF8, buffer, startIndex);
}
///
/// 以字节数组的形式获取指定编码的字符串。
///
/// 要转换的字符串。
/// 要使用的编码。
/// 用于存放结果的字节数组。
public static byte[] GetBytes(string value, Encoding encoding)
{
if (value == null)
{
throw new GameFrameworkException("Value is invalid.");
}
if (encoding == null)
{
throw new GameFrameworkException("Encoding is invalid.");
}
return encoding.GetBytes(value);
}
///
/// 以字节数组的形式获取指定编码的字符串。
///
/// 要转换的字符串。
/// 要使用的编码。
/// 用于存放结果的字节数组。
/// buffer 内实际填充了多少字节。
public static int GetBytes(string value, Encoding encoding, byte[] buffer)
{
return GetBytes(value, encoding, buffer, 0);
}
///
/// 以字节数组的形式获取指定编码的字符串。
///
/// 要转换的字符串。
/// 要使用的编码。
/// 用于存放结果的字节数组。
/// buffer 内的起始位置。
/// buffer 内实际填充了多少字节。
public static int GetBytes(string value, Encoding encoding, byte[] buffer, int startIndex)
{
if (value == null)
{
throw new GameFrameworkException("Value is invalid.");
}
if (encoding == null)
{
throw new GameFrameworkException("Encoding is invalid.");
}
return encoding.GetBytes(value, 0, value.Length, buffer, startIndex);
}
///
/// 返回由字节数组使用 UTF-8 编码转换成的字符串。
///
/// 字节数组。
/// 转换后的字符串。
public static string GetString(byte[] value)
{
return GetString(value, Encoding.UTF8);
}
///
/// 返回由字节数组使用指定编码转换成的字符串。
///
/// 字节数组。
/// 要使用的编码。
/// 转换后的字符串。
public static string GetString(byte[] value, Encoding encoding)
{
if (value == null)
{
throw new GameFrameworkException("Value is invalid.");
}
if (encoding == null)
{
throw new GameFrameworkException("Encoding is invalid.");
}
return encoding.GetString(value);
}
///
/// 返回由字节数组使用 UTF-8 编码转换成的字符串。
///
/// 字节数组。
/// value 内的起始位置。
/// 长度。
/// 转换后的字符串。
public static string GetString(byte[] value, int startIndex, int length)
{
return GetString(value, startIndex, length, Encoding.UTF8);
}
///
/// 返回由字节数组使用指定编码转换成的字符串。
///
/// 字节数组。
/// value 内的起始位置。
/// 长度。
/// 要使用的编码。
/// 转换后的字符串。
public static string GetString(byte[] value, int startIndex, int length, Encoding encoding)
{
if (value == null)
{
throw new GameFrameworkException("Value is invalid.");
}
if (encoding == null)
{
throw new GameFrameworkException("Encoding is invalid.");
}
return encoding.GetString(value, startIndex, length);
}
}
}
}