c# Controla o dispositivo de comando de dados hexadecimais através do método udp

1. Método de conversão de dados

(1), Converter hexadecimal em byte 

  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;
    }

  (2) Converter bytes em dados hexadecimais

 public static string byteToHexStr(byte[] bytes)
    {
        string returnStr = "";
        if (bytes != null)
        {
            for (int i = 0; i < bytes.Length; i++)
            {
                returnStr += bytes[i].ToString("X2");
            }
        }
        return returnStr;
    }

Acho que você gosta

Origin blog.csdn.net/weixin_38826167/article/details/127011568
Recomendado
Clasificación