CalculatorTest.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using AutoMapper.Execution;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Xunit;
  9. namespace HengshanPaymentTerminal.Test
  10. {
  11. public class CalculatorTest
  12. {
  13. [Fact]
  14. public void crc16()
  15. {
  16. byte[] bytes = { 0x12,0x66,0xfb};
  17. ushort value = HengshanCRC16.ComputeChecksum(bytes);
  18. byte[] bytes1 = HengshanCRC16.ComputeChecksumToBytes(bytes);
  19. Console.WriteLine($"CRC TEST RESULT:{value}");
  20. }
  21. [Fact]
  22. public void testByte2Int()
  23. {
  24. int int1 = Bcd2Int(0x00, 0x00 );
  25. int int2 = Bcd2Int(0x00, 0x01 );
  26. int int3 = Bcd2Int(0x00, 0x09 );
  27. int int4 = Bcd2Int(0x00, 0x10 );
  28. int int5 = Bcd2Int(0x01, 0x10 );
  29. int int6 = Bcd2Int(0x09, 0x10 );
  30. int int7 = Bcd2Int(0x10, 0x10 );
  31. int int8 = Bcd2Int(0x99, 0x99 );
  32. Console.WriteLine();
  33. }
  34. public int Bcd2Int(byte byte1, byte byte2)
  35. {
  36. // 提取第一个字节的高四位和低四位
  37. int digit1 = (byte1 >> 4) & 0x0F; // 高四位
  38. int digit2 = byte1 & 0x0F; // 低四位
  39. // 提取第二个字节的高四位和低四位
  40. int digit3 = (byte2 >> 4) & 0x0F; // 高四位
  41. int digit4 = byte2 & 0x0F; // 低四位
  42. // 组合成一个整数
  43. int result = digit1 * 1000 + digit2 * 100 + digit3 * 10 + digit4;
  44. return result;
  45. }
  46. [Fact]
  47. public void testInt2Bytes()
  48. {
  49. byte[] byte1 = Int2Bytes(0);
  50. byte[] byte2 = Int2Bytes(01);
  51. byte[] byte3 = Int2Bytes(99);
  52. byte[] byte4 = Int2Bytes(100);
  53. byte[] byte5 = Int2Bytes(9999);
  54. Console.WriteLine();
  55. }
  56. public byte[] Int2Bytes(int number)
  57. {
  58. // 提取千位、百位、十位和个位
  59. int thousands = number / 1000;
  60. int hundreds = (number / 100) % 10;
  61. int tens = (number / 10) % 10;
  62. int units = number % 10;
  63. // 将千位和百位组合成一个字节(千位在高四位,百位在低四位)
  64. byte firstByte = (byte)((thousands * 16) + hundreds); // 乘以16相当于左移4位
  65. // 将十位和个位组合成一个字节(十位在高四位,个位在低四位)
  66. byte secondByte = (byte)((tens * 16) + units);
  67. // 返回结果数组
  68. return new byte[] { firstByte, secondByte };
  69. }
  70. }
  71. }