SerialInit.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace MOKA_Factory_Tools
  6. {
  7. class SerialInit
  8. {
  9. public static string ByteToHex(byte[] comByte)
  10. {
  11. StringBuilder builder = new StringBuilder(comByte.Length * 3);
  12. foreach (byte data in comByte)
  13. {
  14. builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));
  15. }
  16. return builder.ToString().ToUpper();
  17. }
  18. public static byte[] HexToByte(string msg)
  19. {
  20. msg = msg.Replace(" ", "");//移除空格
  21. //create a byte array the length of the
  22. //divided by 2 (Hex is 2 characters in length)
  23. byte[] comBuffer = new byte[msg.Length / 2];
  24. for (int i = 0; i < msg.Length; i += 2)
  25. {
  26. //convert each set of 2 characters to a byte and add to the array
  27. comBuffer[i / 2] = (byte)Convert.ToByte(msg.Substring(i, 2), 16);
  28. }
  29. return comBuffer;
  30. }
  31. public static bool BytesCompare_Base64(byte[] b1, byte[] b2)
  32. {
  33. if (b1 == null || b2 == null) return false;
  34. if (b1.Length != b2.Length) return false;
  35. return string.Compare(Convert.ToBase64String(b1), Convert.ToBase64String(b2), false) == 0 ? true : false;
  36. }
  37. }
  38. }