You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.8 KiB
C#

using DotNetty.Buffers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CommonExtend
{
public static class ByteBufferExtend
{
public static string ReadHex(this IByteBuffer buffer, int length)
{
IByteBuffer Temp = Unpooled.Buffer(length);
buffer.ReadBytes(Temp, length);
var Result = Temp.Array.Byte2HexString();
Temp.Clear();
return Result;
}
public static void WriteHex(this IByteBuffer buffer, string value,int length)
{
var Temp = value.HexString2Byte().BytePadLeft(length);
buffer.WriteBytes(Temp);
}
public static int ReadInt(this IByteBuffer buffer, int length)
{
IByteBuffer Temp = Unpooled.Buffer(length);
buffer.ReadBytes(Temp, length);
var Result = Temp.Array.Byte2Int();
Temp.Clear();
return Result;
}
public static void WriteInt(this IByteBuffer buffer, int value,int length) {
var Temp = value.Int2Byte().BytePadLeft(length);
buffer.WriteBytes(Temp);
}
public static int[] ReadBitArray(this IByteBuffer buffer)
{
IByteBuffer Temp = Unpooled.Buffer(1);
buffer.ReadBytes(Temp, 1);
var Temp1 = Temp.Array.Byte2BitArray();
var Result = new int[] {
Temp1[0]?1:0,
Temp1[1]?1:0,
Temp1[2]?1:0,
Temp1[3]?1:0,
Temp1[4]?1:0,
Temp1[5]?1:0,
Temp1[6]?1:0,
Temp1[7]?1:0
};
Temp.Clear();
return Result;
}
}
}