submodule对gitea不管用,所以直接拉了一份拉格兰
This commit is contained in:
35
Lagrange.Core/Lagrange.Core.Test/Utility/Tlv.cs
Normal file
35
Lagrange.Core/Lagrange.Core.Test/Utility/Tlv.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System.Text;
|
||||
using Lagrange.Core.Utility.Extension;
|
||||
using static Lagrange.Core.Utility.Binary.BitConverter;
|
||||
|
||||
namespace Lagrange.Core.Test.Utility;
|
||||
|
||||
public static class Tlv
|
||||
{
|
||||
public static Dictionary<string, string> GetTlvDictionary(byte[] tlvs, bool isCommand = true)
|
||||
{
|
||||
var result = new Dictionary<string, string>();
|
||||
|
||||
using var reader = new BinaryReader(new MemoryStream(tlvs));
|
||||
|
||||
ushort command;
|
||||
if (isCommand)
|
||||
{
|
||||
command = ToUInt16(reader.ReadBytes(2), false);
|
||||
}
|
||||
|
||||
ushort tlvCount = ToUInt16(reader.ReadBytes(2), false);
|
||||
|
||||
for (int i = 0; i < tlvCount; i++)
|
||||
{
|
||||
ushort tlvTag = ToUInt16(reader.ReadBytes(2), false);
|
||||
ushort tlvLength = ToUInt16(reader.ReadBytes(2), false);
|
||||
byte[] tlvValue = reader.ReadBytes(tlvLength);
|
||||
|
||||
result.Add($"0x{tlvTag:X} {tlvLength}", tlvValue.Hex());
|
||||
result.Add($"0x{tlvTag:X} UTF8 {tlvLength}", Encoding.UTF8.GetString(tlvValue));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user