123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- 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 = { 0x12,0x66,0xfb};
- ushort value = HengshanCRC16.ComputeChecksum(bytes);
- byte[] bytes1 = HengshanCRC16.ComputeChecksumToBytes(bytes);
- Console.WriteLine($"CRC TEST RESULT:{value}");
- }
- [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 };
- }
- }
- }
|