|
|
@@ -18,6 +18,8 @@ namespace JT809.Protocol.JT809Extensions |
|
|
|
|
|
|
|
private static ulong[] CRC = new ulong[256]; //建立CRC16表 |
|
|
|
|
|
|
|
private static readonly DateTime UTCBaseTime = new DateTime(1970, 1, 1); |
|
|
|
|
|
|
|
static JT809BinaryExtensions() |
|
|
|
{ |
|
|
|
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); |
|
|
@@ -117,6 +119,18 @@ namespace JT809.Protocol.JT809Extensions |
|
|
|
return dateTime; |
|
|
|
} |
|
|
|
|
|
|
|
public static DateTime ReadUTCDateTimeLittle(ReadOnlySpan<byte> buf, ref int offset) |
|
|
|
{ |
|
|
|
ulong result = 0; |
|
|
|
for (int i = 0; i < 8; i++) |
|
|
|
{ |
|
|
|
ulong currentData = (ulong)buf[offset+i] << (8 * (8 - i - 1)); |
|
|
|
result += currentData; |
|
|
|
} |
|
|
|
offset += 8; |
|
|
|
return UTCBaseTime.AddSeconds(result).AddHours(8); |
|
|
|
} |
|
|
|
|
|
|
|
public static int ReadInt32Little(ReadOnlySpan<byte> read, ref int offset) |
|
|
|
{ |
|
|
|
int value = (read[offset] << 24) | (read[offset + 1] << 16) | (read[offset + 2] << 8) | read[offset + 3]; |
|
|
@@ -367,6 +381,18 @@ namespace JT809.Protocol.JT809Extensions |
|
|
|
return len; |
|
|
|
} |
|
|
|
|
|
|
|
public static int WriteUTCDateTimeLittle(IMemoryOwner<byte> memoryOwner, int offset, DateTime date) |
|
|
|
{ |
|
|
|
ulong totalSecends = (ulong)(date.AddHours(-8) - UTCBaseTime).TotalSeconds; |
|
|
|
//高位在前 |
|
|
|
for (int i = 7; i >= 0; i--) |
|
|
|
{ |
|
|
|
memoryOwner.Memory.Span[offset+i] = (byte)(totalSecends & 0xFF); //取低8位 |
|
|
|
totalSecends = totalSecends >> 8; |
|
|
|
} |
|
|
|
return 8; |
|
|
|
} |
|
|
|
|
|
|
|
public static IEnumerable<byte> ToBytes(this string data, Encoding coding) |
|
|
|
{ |
|
|
|
return coding.GetBytes(data); |
|
|
|