CalculatorTest.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 = { 0x00, 0x00, 0x01, 0x00, 0x01, 0x30};
  17. ushort value = HengshanCRC16.ComputeChecksum(bytes);
  18. byte[] bytes1 = HengshanCRC16.ComputeChecksumToBytes(bytes);
  19. Console.WriteLine($"CRC TEST RESULT:{value},hex:{BitConverter.ToString(bytes1).Replace("-"," ")}");
  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. [Fact]
  71. private void stringFromate()
  72. {
  73. byte[] timeBytes = { 0x20, 0x24, 0x12, 0x09, 0x14, 0x02, 0x11 };
  74. string time = 0x20.ToString("X2");
  75. Console.WriteLine(time);
  76. }
  77. [Fact]
  78. private void bytes2DateTime()
  79. {
  80. string formate = "yyyyMMddHHmmss";
  81. string timeStr = "20241209141011";
  82. DateTime parsedDateTime;
  83. bool success = DateTime.TryParseExact(timeStr, formate, null, System.Globalization.DateTimeStyles.None, out parsedDateTime);
  84. Console.WriteLine(parsedDateTime);
  85. }
  86. [Fact]
  87. private void testNumber2Bytes()
  88. {
  89. byte[] bytes = HengshanPayTermHandler.NumberToByteArrayWithPadding(123123, 5);
  90. string result = BitConverter.ToString(bytes).Replace("-", "");
  91. Console.WriteLine(result);
  92. }
  93. [Fact]
  94. private void testFormatDecimal()
  95. {
  96. byte[] bytes1 = HengshanPayTermHandler.FormatDecimal(new decimal(12.1));
  97. string result1 = BitConverter.ToString(bytes1).Replace("-", "");
  98. Console.WriteLine(result1);
  99. byte[] bytes2 = HengshanPayTermHandler.FormatDecimal(new decimal(12.10));
  100. string result2 = BitConverter.ToString(bytes2).Replace("-", "");
  101. Console.WriteLine(result2);
  102. byte[] bytes3 = HengshanPayTermHandler.FormatDecimal(new decimal(12.54));
  103. string result3 = BitConverter.ToString(bytes3).Replace("-", "");
  104. Console.WriteLine(result3);
  105. }
  106. [Fact]
  107. private void testConverDateTimeToBytes()
  108. {
  109. DateTime dateTime = DateTime.Now;
  110. byte[] bytes = HengshanPayTermHandler.ConvertDateTimeToByteArray(dateTime);
  111. string result = BitConverter.ToString(bytes).Replace("-", " ");
  112. Console.WriteLine(result);
  113. }
  114. }
  115. }