using AutoMapper.Execution; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Xunit; namespace HengshanPaymentTerminal.Test { public class CalculatorTest { [Fact] public void crc16() { byte[] bytes = { 0x00, 0x00, 0x01, 0x00, 0x01, 0x30}; ushort value = HengshanCRC16.ComputeChecksum(bytes); byte[] bytes1 = HengshanCRC16.ComputeChecksumToBytes(bytes); Console.WriteLine($"CRC TEST RESULT:{value},hex:{BitConverter.ToString(bytes1).Replace("-"," ")}"); } [Fact] public void testByte2Int() { int int1 = Bcd2Int(0x00, 0x00 ); int int2 = Bcd2Int(0x00, 0x01 ); int int3 = Bcd2Int(0x00, 0x09 ); int int4 = Bcd2Int(0x00, 0x10 ); int int5 = Bcd2Int(0x01, 0x10 ); int int6 = Bcd2Int(0x09, 0x10 ); int int7 = Bcd2Int(0x10, 0x10 ); int int8 = Bcd2Int(0x99, 0x99 ); Console.WriteLine(); } public int Bcd2Int(byte byte1, byte byte2) { // 提取第一个字节的高四位和低四位 int digit1 = (byte1 >> 4) & 0x0F; // 高四位 int digit2 = byte1 & 0x0F; // 低四位 // 提取第二个字节的高四位和低四位 int digit3 = (byte2 >> 4) & 0x0F; // 高四位 int digit4 = byte2 & 0x0F; // 低四位 // 组合成一个整数 int result = digit1 * 1000 + digit2 * 100 + digit3 * 10 + digit4; return result; } [Fact] public void testInt2Bytes() { byte[] byte1 = Int2Bytes(0); byte[] byte2 = Int2Bytes(01); byte[] byte3 = Int2Bytes(99); byte[] byte4 = Int2Bytes(100); byte[] byte5 = Int2Bytes(9999); Console.WriteLine(); } public byte[] Int2Bytes(int number) { // 提取千位、百位、十位和个位 int thousands = number / 1000; int hundreds = (number / 100) % 10; int tens = (number / 10) % 10; int units = number % 10; // 将千位和百位组合成一个字节(千位在高四位,百位在低四位) byte firstByte = (byte)((thousands * 16) + hundreds); // 乘以16相当于左移4位 // 将十位和个位组合成一个字节(十位在高四位,个位在低四位) byte secondByte = (byte)((tens * 16) + units); // 返回结果数组 return new byte[] { firstByte, secondByte }; } [Fact] private void stringFromate() { byte[] timeBytes = { 0x20, 0x24, 0x12, 0x09, 0x14, 0x02, 0x11 }; string time = 0x20.ToString("X2"); Console.WriteLine(time); } [Fact] private void bytes2DateTime() { string formate = "yyyyMMddHHmmss"; string timeStr = "20241209141011"; DateTime parsedDateTime; bool success = DateTime.TryParseExact(timeStr, formate, null, System.Globalization.DateTimeStyles.None, out parsedDateTime); Console.WriteLine(parsedDateTime); } [Fact] private void testNumber2Bytes() { byte[] bytes = HengshanPayTermHandler.NumberToByteArrayWithPadding(123123, 5); string result = BitConverter.ToString(bytes).Replace("-", ""); Console.WriteLine(result); } [Fact] private void testFormatDecimal() { byte[] bytes1 = HengshanPayTermHandler.FormatDecimal(new decimal(12.1)); string result1 = BitConverter.ToString(bytes1).Replace("-", ""); Console.WriteLine(result1); byte[] bytes2 = HengshanPayTermHandler.FormatDecimal(new decimal(12.10)); string result2 = BitConverter.ToString(bytes2).Replace("-", ""); Console.WriteLine(result2); byte[] bytes3 = HengshanPayTermHandler.FormatDecimal(new decimal(12.54)); string result3 = BitConverter.ToString(bytes3).Replace("-", ""); Console.WriteLine(result3); } [Fact] private void testConverDateTimeToBytes() { DateTime dateTime = DateTime.Now; byte[] bytes = HengshanPayTermHandler.ConvertDateTimeToByteArray(dateTime); string result = BitConverter.ToString(bytes).Replace("-", " "); Console.WriteLine(result); } } }