1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace MOKA_Factory_Tools
- {
- class SerialInit
- {
- public static string ByteToHex(byte[] comByte)
- {
- StringBuilder builder = new StringBuilder(comByte.Length * 3);
- foreach (byte data in comByte)
- {
- builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));
- }
- return builder.ToString().ToUpper();
- }
- public static byte[] HexToByte(string msg)
- {
- msg = msg.Replace(" ", "");//移除空格
- //create a byte array the length of the
- //divided by 2 (Hex is 2 characters in length)
- byte[] comBuffer = new byte[msg.Length / 2];
- for (int i = 0; i < msg.Length; i += 2)
- {
- //convert each set of 2 characters to a byte and add to the array
- comBuffer[i / 2] = (byte)Convert.ToByte(msg.Substring(i, 2), 16);
- }
- return comBuffer;
- }
- public static bool BytesCompare_Base64(byte[] b1, byte[] b2)
- {
- if (b1 == null || b2 == null) return false;
- if (b1.Length != b2.Length) return false;
- return string.Compare(Convert.ToBase64String(b1), Convert.ToBase64String(b2), false) == 0 ? true : false;
- }
- /// <summary>
- /// 将16进制字符串转成字节;
- /// </summary>
- /// <param name="hexString"></param>
- /// <returns></returns>
- public static byte[] strToToHexByte(string hexString)
- {
- hexString = hexString.Replace(" ", "");
- if ((hexString.Length % 2) != 0)
- hexString += " ";
- byte[] returnBytes = new byte[hexString.Length / 2];
- for (int i = 0; i < returnBytes.Length; i++)
- returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
- return returnBytes;
- }
- }
- }
|