📄 binaryhelper.cs
字号:
namespace NCindy.Protocol.RUDP.Stack
{
using System;
using System.Runtime.InteropServices;
using System.Text;
internal static class BinaryHelper
{
internal static int GetByteCount(string s)
{
return (Encoding.UTF8.GetByteCount(s) + 1);
}
internal static bool ReadBool(byte[] bin, int offset)
{
return (bin[offset] > 0);
}
internal static bool ReadFlag(byte[] bin, int offset)
{
byte num = (byte) (0x80 & bin[offset]);
return (num == 0x80);
}
internal static float ReadFloat(byte[] bin, int offset)
{
if (BitConverter.IsLittleEndian)
{
SwapEndianism(bin, offset, 4);
float num = BitConverter.ToSingle(bin, offset);
SwapEndianism(bin, offset, 4);
return num;
}
return BitConverter.ToSingle(bin, offset);
}
internal static int ReadInt(byte[] bin, int offset)
{
int num = 0;
for (int i = 0; i < 4; i++)
{
num = (num << 8) | bin[i + offset];
}
return num;
}
internal static long ReadLong(byte[] bin, int offset)
{
long num = 0;
for (int i = 0; i < 8; i++)
{
num = (num << 8) | bin[i + offset];
}
return num;
}
internal static short ReadShort(byte[] bin, int offset)
{
return (short) ((bin[offset] << 8) | bin[offset + 1]);
}
internal static string ReadString(byte[] bin, int offset, out int bytelength)
{
int index = offset;
while (bin[index] != 0)
{
index++;
}
bytelength = (index - offset) + 1;
return Encoding.UTF8.GetString(bin, offset, bytelength - 1);
}
private static void SwapEndianism(byte[] data, int offset, int length)
{
int num = length / 2;
for (int i = 0; i < num; i++)
{
byte num3 = data[offset + i];
data[offset + i] = data[((offset + length) - i) - 1];
data[((offset + length) - i) - 1] = num3;
}
}
internal static void WriteBool(bool val, byte[] target, int offset)
{
if (val)
{
target[offset] = 1;
}
else
{
target[offset] = 0;
}
}
internal static void WriteFlag(bool flag, byte[] target, int offset)
{
byte num = target[offset];
if (flag)
{
num = (byte) (num | 0x80);
}
else
{
num = (byte) (num & 0x7f);
}
target[offset] = num;
}
internal static void WriteFloat(float value, byte[] target, int offset)
{
byte[] data = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian)
{
SwapEndianism(data, 0, 4);
}
Array.Copy(data, 0, target, offset, 4);
}
internal static void WriteInt(int val, byte[] target, int offset)
{
for (int i = 0; i < 4; i++)
{
target[offset + i] = (byte) (0xff & (val >> (8 * (3 - i))));
}
}
internal static void WriteLong(long lval, byte[] target, int offset)
{
for (int i = 0; i < 8; i++)
{
byte num2 = (byte) (0xff & (lval >> (8 * (7 - i))));
target[i + offset] = num2;
}
}
internal static void WriteShort(short val, byte[] target, int offset)
{
target[offset] = (byte) (0xff & (val >> 8));
target[offset + 1] = (byte) (0xff & val);
}
internal static int WriteString(string svalue, byte[] target, int offset)
{
int num = Encoding.UTF8.GetBytes(svalue, 0, svalue.Length, target, offset);
target[offset + num] = 0;
return (num + 1);
}
internal static void WriteUInt(uint val, byte[] target, int offset)
{
for (int i = 0; i < 4; i++)
{
target[offset + i] = (byte) (0xff & (val >> (8 * (3 - i))));
}
}
internal static void WriteUShort(ushort val, byte[] target, int offset)
{
target[offset] = (byte) (0xff & (val >> 8));
target[offset + 1] = (byte) (0xff & val);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -