fork download
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. public class Test
  9. {
  10. public static void Main()
  11. {
  12. //原始資料
  13. string byteArrayString = "FF0133000104D2EA4104418501050C1E01010001";
  14. byte[] byteArrayInput = ConvertHexStringToByteArray(byteArrayString);
  15.  
  16. BitArray result = new BitArray(byteArrayInput.Length);
  17. //取前2 xor
  18. char[] strArray = byteArrayString.ToCharArray();
  19. for (int i = 0; i < strArray.Length; i=i+2)
  20. {
  21. //測試用,先不判斷長度
  22. try
  23. {
  24. string inputA = string.Format("{0}{1}", strArray[i], strArray[i + 1]);
  25. string inputB = string.Format("{0}{1}", strArray[i + 2], strArray[i + 3]);
  26. if (i == 0)
  27. {
  28. BitArray input1 = new BitArray(ConvertHexStringToByteArray(inputA));
  29. BitArray input2 = new BitArray(ConvertHexStringToByteArray(inputB));
  30. Console.WriteLine(inputA + "^" + inputB);
  31. result = input1.Xor(input2);
  32. }
  33. else
  34. {
  35. BitArray input1 = result;
  36. BitArray input2 = new BitArray(ConvertHexStringToByteArray(inputB));
  37. Console.WriteLine(inputA + "^" + inputB);
  38. result = input1.Xor(input2);
  39.  
  40. }
  41.  
  42. }
  43. catch(Exception ex) {
  44. Console.WriteLine(ex.Message);
  45. }
  46.  
  47. }
  48. byte[] output = new byte[result.Length];
  49. result.CopyTo(output,0);
  50. Console.WriteLine(BitConverter.ToString(output));
  51. Console.WriteLine(BitConverter.ToInt32(output,0));
  52.  
  53. Console.ReadLine(); //Pause
  54.  
  55.  
  56. }
  57. public static string ByteArrayToString(byte[] ba)
  58. {
  59. string hex = BitConverter.ToString(ba);
  60. return hex.Replace("-", "");
  61. }
  62. /// <summary>
  63. /// Shifts the bits in an array of bytes to the left.
  64. /// </summary>
  65. /// <param name="bytes">The byte array to shift.</param>
  66. public static bool ShiftLeft(byte[] bytes)
  67. {
  68. bool leftMostCarryFlag = false;
  69.  
  70. // Iterate through the elements of the array from left to right.
  71. for (int index = 0; index < bytes.Length; index++)
  72. {
  73. // If the leftmost bit of the current byte is 1 then we have a carry.
  74. bool carryFlag = (bytes[index] & 0x80) > 0;
  75.  
  76. if (index > 0)
  77. {
  78. if (carryFlag == true)
  79. {
  80. // Apply the carry to the rightmost bit of the current bytes neighbor to the left.
  81. bytes[index - 1] = (byte)(bytes[index - 1] | 0x01);
  82. }
  83. }
  84. else
  85. {
  86. leftMostCarryFlag = carryFlag;
  87. }
  88.  
  89. bytes[index] = (byte)(bytes[index] << 1);
  90. }
  91.  
  92. return leftMostCarryFlag;
  93. }
  94. private static byte[] ConvertHexStringToByteArray(string hexString)
  95. {
  96. if (hexString.Length % 2 != 0)
  97. {
  98. throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));
  99. }
  100.  
  101. byte[] HexAsBytes = new byte[hexString.Length / 2];
  102. for (int index = 0; index < HexAsBytes.Length; index++)
  103. {
  104. string byteValue = hexString.Substring(index * 2, 2);
  105. HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
  106. }
  107.  
  108. return HexAsBytes;
  109. }
  110.  
  111.  
  112. }
Success #stdin #stdout 0.04s 24232KB
stdin
Standard input is empty
stdout
FF^01
01^33
33^00
00^01
01^04
04^D2
D2^EA
EA^41
41^04
04^41
41^85
85^01
01^05
05^0C
0C^1E
1E^01
01^01
01^00
00^01
Array index is out of range.
66-00-00-00-00-00-00-00
102