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

257 lines
7.2 KiB

8 months ago
// See https://aka.ms/new-console-template for more information
8 months ago
using System.Collections;
using ConsoleStarter;
8 months ago
using HybirdFrameworkCore.Autofac.Attribute;
8 months ago
using HybirdFrameworkCore.Utils;
internal class Program
{
public static void Main(string[] args)
{
TestPerson testPerson = new TestPerson();
testPerson.intParam = 11;
testPerson.intParamNull = 12;
testPerson.uit = 13;
testPerson.byteParam = 2;
testPerson._sb = 3;
testPerson.s = "abcdef";
testPerson.st = -15;
testPerson.ust = 15;
testPerson.I16 = -16;
testPerson.UI16 = 16
;
testPerson.I32 = -32;
testPerson.UI32 = 32;
//testPerson.I64 = -64;
//testPerson.UI64 = 64;
//testPerson.lg = -100;
//testPerson.ulg = 100;
testPerson.ft = 12.11f;
testPerson.de = -12.12d;
// testPerson.de2 = -12.12d;
// testPerson.bl = false;
testPerson.ft2 = -13.126f;
8 months ago
testPerson.ft3 = 1000f;
testPerson.d3 = -13.129d;
byte[] bytes2 = ModelConvert.Encode(testPerson);
TestPerson testPerson1 = ModelConvert.Declode<TestPerson>(bytes2);
int a = testPerson1.intParam;
int b9 = (int)testPerson1.intParamNull;
uint c = testPerson1.uit;
byte d = testPerson1.byteParam;
byte e = testPerson1._sb;
string f = testPerson1.s;
short a1 = testPerson1.st;
ushort a2 = testPerson1.ust;
Int16 a3 = testPerson1.I16;
UInt16 a4 = testPerson1.UI16;
Int32 b1 = testPerson1.I32;
UInt32 b2 = testPerson1.UI32;
//Int64 b3 = testPerson1.I64;
//UInt64 b4 = testPerson1.UI64;
//long b5 = testPerson1.lg;
//ulong c1 = testPerson1.ulg;
float c2 = testPerson1.ft;
double c3 = testPerson1.de;
//double c6 = testPerson1.de2;
//bool c4 = testPerson1.bl;
float c5 = testPerson1.ft2;
8 months ago
// 偏移量
float c6 = testPerson1.ft3;
// double负数转换
double d3 = testPerson1.d3;
double d4 = -d3;
8 months ago
List<PropertyAttribute> list = new List<PropertyAttribute>
{
new(0, 4),
new(4, 8),
new(12, 1),
new(13, 2),
new(15, 1),
new(16, 1),
new(17, 1),
new(18, 2),
new(20, 2),
new(22, 2),
new(24, 2),
new(26, 1),
new(27, 2),
new(29, 2),
new(31, 1),
new(32, 1),
new(33, 29)
};
int start = 0;
int length = 0;
foreach (PropertyAttribute attribute in list)
{
if (start < attribute.Start)
{
start = attribute.Start;
length = attribute.Length;
}
}
8 months ago
8 months ago
Console.WriteLine($"{start}, {length}");
8 months ago
8 months ago
byte[] bytes = new Byte[] { 0xFE, 0xFF };
ushort uInt16 = BitConverter.ToUInt16(bytes);
Console.WriteLine($"{uInt16}, {BitUtls.BytesToHexStr(bytes)}");
BitArray bitArray = new BitArray(16);
int index = 0;
foreach (var b in bytes)
8 months ago
{
8 months ago
for (int i = 0; i < 8; i++)
8 months ago
{
8 months ago
bool flag = (b & (1 << (index % 8))) > 0;
Console.WriteLine(flag);
bitArray[index++] = flag;
8 months ago
}
8 months ago
}
byte[] newbytes = new byte[2];
bitArray.CopyTo(newbytes, 0);
foreach (var b in newbytes)
{
Console.WriteLine(b);
}
ushort int16 = BitConverter.ToUInt16(newbytes);
Console.WriteLine($"{int16}, {BitUtls.BytesToHexStr(newbytes)}");
8 months ago
UInt64 max = UInt64.MaxValue / 8;
byte[] bytes1 = BitConverter.GetBytes(max);
Console.WriteLine($"{max}, {BitUtls.BytesToHexStr(bytes1)}");
foreach (var b in bytes1)
{
Console.WriteLine(b);
}
BitArray array = new BitArray(16);
array[0] = true;
byte[] ab = new byte[2];
array.CopyTo(ab, 0);
foreach (var b in ab)
{
Console.WriteLine(b);
8 months ago
}
}
private static void Test1()
{
Console.WriteLine("Hello, World!");
List<PropertyAttribute> list = new List<PropertyAttribute>
{
new(0, 4),
new(4, 8),
new(12, 1),
new(13, 2),
new(15, 1),
new(16, 1),
new(17, 1),
new(18, 2),
new(20, 2),
new(22, 2),
new(24, 2),
new(26, 1),
new(27, 2),
new(29, 2),
new(31, 1),
new(32, 1),
new(33, 29)
};
8 months ago
8 months ago
Queue<EncodeData> queue = new Queue<EncodeData>();
int size = 0;
foreach (PropertyAttribute p in list)
{
size += p.Length;
if (size < 8)
{
queue.Enqueue(new EncodeData()
{
Start = 0,
Length = p.Length
8 months ago
});
}
else
8 months ago
{
8 months ago
Console.WriteLine("---------------------------begin one byte---------------------");
int length = 0;
while (queue.Count > 0)
{
EncodeData data = queue.Dequeue();
length += data.Length;
Console.WriteLine($"use {data.Start}, {data.Length}");
}
size -= length;
int occupy = 8 - length;
Console.WriteLine($"use 0, {occupy}");
Console.WriteLine("---------------------------end one byte---------------------");
size -= occupy;
while (size >= 8)
{
Console.WriteLine("---------------------------begin one byte---------------------");
Console.WriteLine($"use {occupy}, 8");
Console.WriteLine("---------------------------end one byte---------------------");
size -= 8;
occupy += 8;
}
if (size > 0)
{
queue.Enqueue(new EncodeData()
{
Start = occupy,
Length = size
8 months ago
});
}
8 months ago
}
8 months ago
}
8 months ago
8 months ago
foreach (EncodeData d in queue)
{
Console.WriteLine($"{d.Start}, {d.Length}");
8 months ago
}
8 months ago
UInt32 i = UInt32.MaxValue - 1;
byte[] bytes = BitConverter.GetBytes(i).Reverse().ToArray();
foreach (byte b in bytes)
{
Console.WriteLine(b);
}
Console.WriteLine(BitUtls.BytesToHexStr(bytes));
UInt32 r = BitConverter.ToUInt32(bytes);
Console.WriteLine(r);
bytes = BitConverter.GetBytes(r);
foreach (byte b in bytes)
{
Console.WriteLine(b);
}
Console.WriteLine(BitUtls.BytesToHexStr(bytes));
8 months ago
}
}
8 months ago
public struct EncodeData
{
public int Start { get; set; }
public int Length { get; set; }
public byte[] Value { get; set; }
}