//------------------------------------------------------------------------------
// 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
// CSDN博客:https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频:https://space.bilibili.com/94253567
// Gitee源代码仓库:https://gitee.com/RRQM_Home
// Github源代码仓库:https://github.com/RRQM
// API首页:https://www.yuque.com/rrqm/touchsocket/index
// 交流QQ群:234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using System;
using System.Runtime.CompilerServices;
namespace TouchSocket.Core
{
///
/// 将基数据类型转换为指定端的一个字节数组,
/// 或将一个字节数组转换为指定端基数据类型。
///
public class TouchSocketBitConverter
{
///
/// 以大端
///
public static TouchSocketBitConverter BigEndian;
///
/// 以小端
///
public static TouchSocketBitConverter LittleEndian;
static TouchSocketBitConverter()
{
BigEndian = new TouchSocketBitConverter(EndianType.Big);
LittleEndian = new TouchSocketBitConverter(EndianType.Little);
DefaultEndianType = EndianType.Little;
}
private static TouchSocketBitConverter m_default;
///
/// 以默认小端,可通过重新指定默认端。
///
public static TouchSocketBitConverter Default => m_default;
private static EndianType m_defaultEndianType;
///
/// 默认大小端切换。
///
public static EndianType DefaultEndianType
{
get => m_defaultEndianType;
set
{
m_defaultEndianType = value;
switch (value)
{
case EndianType.Little:
m_default = LittleEndian;
break;
case EndianType.Big:
m_default = BigEndian;
break;
default:
break;
}
}
}
private readonly EndianType endianType;
///
/// 构造函数
///
///
public TouchSocketBitConverter(EndianType endianType)
{
this.endianType = endianType;
}
///
/// 指定大小端。
///
public EndianType EndianType => endianType;
///
/// 判断当前系统是否为设置的大小端
///
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsSameOfSet()
{
return !(BitConverter.IsLittleEndian ^ (endianType == EndianType.Little));
//return true;
}
#region ushort
///
/// 转换为指定端2字节
///
///
///
public byte[] GetBytes(ushort value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (!IsSameOfSet())
{
Array.Reverse(bytes);
}
return bytes;
}
///
/// 转换为指定端模式的2字节转换为UInt16数据。
///
///
///
///
public ushort ToUInt16(byte[] buffer, int offset)
{
if (IsSameOfSet())
{
return BitConverter.ToUInt16(buffer, offset);
}
else
{
byte[] bytes = new byte[2];
Array.Copy(buffer, offset, bytes, 0, 2);
Array.Reverse(bytes);
return BitConverter.ToUInt16(bytes, 0);
}
}
#endregion ushort
#region ulong
///
/// 转换为指定端8字节
///
///
///
public byte[] GetBytes(ulong value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (!IsSameOfSet())
{
Array.Reverse(bytes);
}
return bytes;
}
///
/// 转换为指定端模式的Ulong数据。
///
///
///
///
public ulong ToUInt64(byte[] buffer, int offset)
{
if (IsSameOfSet())
{
return BitConverter.ToUInt64(buffer, offset);
}
else
{
byte[] bytes = new byte[8];
Array.Copy(buffer, offset, bytes, 0, 8);
Array.Reverse(bytes);
return BitConverter.ToUInt64(bytes,0);
}
}
#endregion ulong
#region bool
///
/// 转换为指定端1字节
///
///
///
public byte[] GetBytes(bool value)
{
return BitConverter.GetBytes(value);
}
///
/// 转换为指定端模式的bool数据。
///
///
///
///
public bool ToBoolean(byte[] buffer, int offset)
{
return BitConverter.ToBoolean(buffer, offset);
}
#endregion bool
#region char
///
/// 转换为指定端2字节
///
///
///
public byte[] GetBytes(char value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (!IsSameOfSet())
{
Array.Reverse(bytes);
}
return bytes;
}
///
/// 转换为指定端模式的Char数据。
///
///
///
///
public char ToChar(byte[] buffer, int offset)
{
if (IsSameOfSet())
{
return BitConverter.ToChar(buffer, offset);
}
else
{
byte[] bytes = new byte[2];
Array.Copy(buffer, offset, bytes, 0, bytes.Length);
Array.Reverse(bytes);
return BitConverter.ToChar(bytes, 0);
}
}
#endregion char
#region short
///
/// 转换为指定端2字节
///
///
///
public byte[] GetBytes(short value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (!IsSameOfSet())
{
Array.Reverse(bytes);
}
return bytes;
}
///
/// 转换为指定端模式的Short数据。
///
///
///
///
public short ToInt16(byte[] buffer, int offset)
{
if (IsSameOfSet())
{
return BitConverter.ToInt16(buffer, offset);
}
else
{
byte[] bytes = new byte[2];
Array.Copy(buffer, offset, bytes, 0, bytes.Length);
Array.Reverse(bytes);
return BitConverter.ToInt16(bytes, 0);
}
}
#endregion short
#region int
///
/// 转换为指定端4字节
///
///
///
public byte[] GetBytes(int value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (!IsSameOfSet())
{
Array.Reverse(bytes);
}
return bytes;
}
///
/// 转换为指定端模式的int数据。
///
///
///
///
public int ToInt32(byte[] buffer, int offset)
{
if (IsSameOfSet())
{
return BitConverter.ToInt32(buffer, offset);
}
else
{
byte[] bytes = new byte[4];
Array.Copy(buffer, offset, bytes, 0, bytes.Length);
Array.Reverse(bytes);
return BitConverter.ToInt32(bytes, 0);
}
}
#endregion int
#region long
///
/// 转换为指定端8字节
///
///
///
public byte[] GetBytes(long value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (!IsSameOfSet())
{
Array.Reverse(bytes);
}
return bytes;
}
///
/// 转换为指定端模式的long数据。
///
///
///
///
public long ToInt64(byte[] buffer, int offset)
{
if (IsSameOfSet())
{
return BitConverter.ToInt64(buffer, offset);
}
else
{
byte[] bytes = new byte[8];
Array.Copy(buffer, offset, bytes, 0, bytes.Length);
Array.Reverse(bytes);
return BitConverter.ToInt64(bytes, 0);
}
}
#endregion long
#region uint
///
/// 转换为指定端4字节
///
///
///
public byte[] GetBytes(uint value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (!IsSameOfSet())
{
Array.Reverse(bytes);
}
return bytes;
}
///
/// 转换为指定端模式的Uint数据。
///
///
///
///
public uint ToUInt32(byte[] buffer, int offset)
{
if (IsSameOfSet())
{
return BitConverter.ToUInt32(buffer, offset);
}
else
{
byte[] bytes = new byte[4];
Array.Copy(buffer, offset, bytes, 0, bytes.Length);
Array.Reverse(bytes);
return BitConverter.ToUInt32(bytes, 0);
}
}
#endregion uint
#region float
///
/// 转换为指定端4字节
///
///
///
public byte[] GetBytes(float value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (!IsSameOfSet())
{
Array.Reverse(bytes);
}
return bytes;
}
///
/// 转换为指定端模式的float数据。
///
///
///
///
public float ToSingle(byte[] buffer, int offset)
{
if (IsSameOfSet())
{
return BitConverter.ToSingle(buffer, offset);
}
else
{
byte[] bytes = new byte[4];
Array.Copy(buffer, offset, bytes, 0, bytes.Length);
Array.Reverse(bytes);
return BitConverter.ToSingle(bytes, 0);
}
}
#endregion float
#region long
///
/// 转换为指定端8字节
///
///
///
public byte[] GetBytes(double value)
{
byte[] bytes = BitConverter.GetBytes(value);
if (!IsSameOfSet())
{
Array.Reverse(bytes);
}
return bytes;
}
///
/// 转换为指定端模式的double数据。
///
///
///
///
public double ToDouble(byte[] buffer, int offset)
{
if (IsSameOfSet())
{
return BitConverter.ToDouble(buffer, offset);
}
else
{
byte[] bytes = new byte[8];
Array.Copy(buffer, offset, bytes, 0, bytes.Length);
Array.Reverse(bytes);
return BitConverter.ToDouble(bytes, 0);
}
}
#endregion long
}
}