Unity 任意字符串转16进制byte数组

    private string ConvertHexString(byte[] ascallByte)
    {
        StringBuilder hex = new StringBuilder();
        for (int i = 0; i < ascallByte.Length; i++)
        {
//10进制ascall数值转16进制,替换成自己的方法,这里是我自己封装的任意转换方法
            hex.Append(MMath.Conversion(ascallByte[i].ToString(), 10, 16, 2));
        }
        return hex.ToString();
    }
    private byte[] Convert16(string strText)
    {
        strText = strText.Replace(" ", "");
        byte[] bText = new byte[strText.Length / 2];
        for (int i = 0; i < strText.Length / 2; i++)
        {
            bText[i] = Convert.ToByte(Convert.ToInt32(strText.Substring(i * 2, 2), 16));
        }
        return bText;
    }
strAscByte = Encoding.ASCII.GetBytes("open");
hexByte = Convert16(ConvertHexString(sendData));

 

你可能感兴趣的:(unity)