fork(3) download
  1. using System;
  2. using System.Linq;
  3.  
  4. public class Test
  5. {
  6. private static void Assert (bool same)
  7. {
  8. Console.WriteLine(same);
  9. }
  10.  
  11. private static byte[] ConvertHexToBytes(string input)
  12. {
  13. var result = new byte[(input.Length + 1) / 2];
  14. var offset = 0;
  15. if (input.Length % 2 == 1) {
  16. // If length of input is odd, the first character has an implicit 0 prepended.
  17. result[0] = (byte)Convert.ToUInt32(input[0] + "", 16);
  18. offset = 1;
  19. }
  20. for (int i = 0; i < input.Length / 2; i++) {
  21. result[i + offset] = (byte)Convert.ToUInt32(input.Substring(i * 2 + offset, 2), 16);
  22. }
  23. return result;
  24. }
  25.  
  26. private static int HexToInt(char c)
  27. {
  28. switch (c) {
  29. case '0':
  30. return 0;
  31. case '1':
  32. return 1;
  33. case '2':
  34. return 2;
  35. case '3':
  36. return 3;
  37. case '4':
  38. return 4;
  39. case '5':
  40. return 5;
  41. case '6':
  42. return 6;
  43. case '7':
  44. return 7;
  45. case '8':
  46. return 8;
  47. case '9':
  48. return 9;
  49. case 'a':
  50. case 'A':
  51. return 10;
  52. case 'b':
  53. case 'B':
  54. return 11;
  55. case 'c':
  56. case 'C':
  57. return 12;
  58. case 'd':
  59. case 'D':
  60. return 13;
  61. case 'e':
  62. case 'E':
  63. return 14;
  64. case 'f':
  65. case 'F':
  66. return 15;
  67. default:
  68. throw new FormatException("Unrecognized hex char " + c);
  69. }
  70. }
  71.  
  72. private static readonly byte[,] ByteLookup = new byte[,]
  73. {
  74. // low nibble
  75. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  76. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
  77. // high nibble
  78. {0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70,
  79. 0x80, 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0}
  80. };
  81.  
  82. private static byte[] ConvertHexToBytesX(string input)
  83. {
  84. var result = new byte[(input.Length + 1) >> 1];
  85. int lastcell = result.Length - 1;
  86. int lastchar = input.Length - 1;
  87. for (int i = 0; i < input.Length; i++) {
  88. result[lastcell - (i >> 1)] |= ByteLookup[i & 1, HexToInt(input[lastchar - i])];
  89. }
  90. return result;
  91. }
  92.  
  93. private static void TestConversions() {
  94. var inputs = new[] { "",
  95. "0",
  96. "10",
  97. "f",
  98. "0f",
  99. "010",
  100. "0ff",
  101. "f2ab"
  102. };
  103. var i = 0;
  104. Assert(ConvertHexToBytes(inputs[i++]).SequenceEqual(new byte[] { }));
  105. Assert(ConvertHexToBytes(inputs[i++]).SequenceEqual(new byte[] { 0 }));
  106. Assert(ConvertHexToBytes(inputs[i++]).SequenceEqual(new byte[] { 0x10 }));
  107. Assert(ConvertHexToBytes(inputs[i++]).SequenceEqual(new byte[] { 0xf }));
  108. Assert(ConvertHexToBytes(inputs[i++]).SequenceEqual(new byte[] { 0xf }));
  109. Assert(ConvertHexToBytes(inputs[i++]).SequenceEqual(new byte[] { 0x0, 0x10 }));
  110. Assert(ConvertHexToBytes(inputs[i++]).SequenceEqual(new byte[] { 0x0, 0xff }));
  111. Assert(ConvertHexToBytes(inputs[i++]).SequenceEqual(new byte[] { 0xf2, 0xab }));
  112. Assert(i == inputs.Length);
  113. Console.WriteLine("rolfl");
  114. i = 0;
  115. Assert(ConvertHexToBytesX(inputs[i++]).SequenceEqual(new byte[] { }));
  116. Assert(ConvertHexToBytesX(inputs[i++]).SequenceEqual(new byte[] { 0 }));
  117. Assert(ConvertHexToBytesX(inputs[i++]).SequenceEqual(new byte[] { 0x10 }));
  118. Assert(ConvertHexToBytesX(inputs[i++]).SequenceEqual(new byte[] { 0xf }));
  119. Assert(ConvertHexToBytesX(inputs[i++]).SequenceEqual(new byte[] { 0xf }));
  120. Assert(ConvertHexToBytesX(inputs[i++]).SequenceEqual(new byte[] { 0x0, 0x10 }));
  121. Assert(ConvertHexToBytesX(inputs[i++]).SequenceEqual(new byte[] { 0x0, 0xff }));
  122. Assert(ConvertHexToBytesX(inputs[i++]).SequenceEqual(new byte[] { 0xf2, 0xab }));
  123. Assert(i == inputs.Length);
  124. }
  125. public static void Main()
  126. {
  127. TestConversions();
  128. }
  129. }
Success #stdin #stdout 0.04s 33920KB
stdin
Standard input is empty
stdout
True
True
True
True
True
True
True
True
True
rolfl
True
True
True
True
True
True
True
True
True