Guest User

Untitled

a guest
May 26th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SessionDeserialize
  8. {
  9. public class HexEncoding
  10. {
  11. public static int GetByteCount(string hexString)
  12. {
  13. int numHexChars = 0;
  14. char c;
  15. // remove all none A-F, 0-9, characters
  16. for (int i = 0; i < hexString.Length; i++)
  17. {
  18. c = hexString[i];
  19. if (IsHexDigit(c))
  20. numHexChars++;
  21. }
  22. // if odd number of characters, discard last character
  23. if (numHexChars % 2 != 0)
  24. {
  25. numHexChars--;
  26. }
  27. return numHexChars / 2; // 2 characters per byte
  28. }
  29. /// <summary>
  30. /// Creates a byte array from the hexadecimal string. Each two characters are combined
  31. /// to create one byte. First two hexadecimal characters become first byte in returned array.
  32. /// Non-hexadecimal characters are ignored.
  33. /// </summary>
  34. /// <param name="hexString">string to convert to byte array</param>
  35. /// <param name="discarded">number of characters in string ignored</param>
  36. /// <returns>byte array, in the same left-to-right order as the hexString</returns>
  37. public static byte[] GetBytes(string hexString, out int discarded)
  38. {
  39. discarded = 0;
  40. string newString = "";
  41. char c;
  42. // remove all none A-F, 0-9, characters
  43. for (int i = 0; i < hexString.Length; i++)
  44. {
  45. c = hexString[i];
  46. if (IsHexDigit(c))
  47. newString += c;
  48. else
  49. discarded++;
  50. }
  51. // if odd number of characters, discard last character
  52. if (newString.Length % 2 != 0)
  53. {
  54. discarded++;
  55. newString = newString.Substring(0, newString.Length - 1);
  56. }
  57.  
  58. int byteLength = newString.Length / 2;
  59. byte[] bytes = new byte[byteLength];
  60. string hex;
  61. int j = 0;
  62. for (int i = 0; i < bytes.Length; i++)
  63. {
  64. hex = new String(new Char[] { newString[j], newString[j + 1] });
  65. bytes[i] = HexToByte(hex);
  66. j = j + 2;
  67. }
  68. return bytes;
  69. }
  70. public static string ToString(byte[] bytes)
  71. {
  72. string hexString = "";
  73. for (int i = 0; i < bytes.Length; i++)
  74. {
  75. hexString += bytes[i].ToString("X2");
  76. }
  77. return hexString;
  78. }
  79. /// <summary>
  80. /// Determines if given string is in proper hexadecimal string format
  81. /// </summary>
  82. /// <param name="hexString"></param>
  83. /// <returns></returns>
  84. public static bool InHexFormat(string hexString)
  85. {
  86. bool hexFormat = true;
  87.  
  88. foreach (char digit in hexString)
  89. {
  90. if (!IsHexDigit(digit))
  91. {
  92. hexFormat = false;
  93. break;
  94. }
  95. }
  96. return hexFormat;
  97. }
  98.  
  99. /// <summary>
  100. /// Returns true is c is a hexadecimal digit (A-F, a-f, 0-9)
  101. /// </summary>
  102. /// <param name="c">Character to test</param>
  103. /// <returns>true if hex digit, false if not</returns>
  104. public static bool IsHexDigit(Char c)
  105. {
  106. int numChar;
  107. int numA = Convert.ToInt32('A');
  108. int num1 = Convert.ToInt32('0');
  109. c = Char.ToUpper(c);
  110. numChar = Convert.ToInt32(c);
  111. if (numChar >= numA && numChar < (numA + 6))
  112. return true;
  113. if (numChar >= num1 && numChar < (num1 + 10))
  114. return true;
  115. return false;
  116. }
  117. /// <summary>
  118. /// Converts 1 or 2 character string into equivalant byte value
  119. /// </summary>
  120. /// <param name="hex">1 or 2 character string</param>
  121. /// <returns>byte</returns>
  122. private static byte HexToByte(string hex)
  123. {
  124. if (hex.Length > 2 || hex.Length <= 0)
  125. throw new ArgumentException("hex must be 1 or 2 characters in length");
  126. byte newByte = byte.Parse(hex, System.Globalization.NumberStyles.HexNumber);
  127. return newByte;
  128. }
  129. }
  130. }
Add Comment
Please, Sign In to add comment