TCP Socket 数据发送和接收时进制转换

发布时间 2023-10-07 11:09:48作者: emanlee

 

 

 

确实经过了转换:十六进制--》十进制--》ASCII字符

十六进制的 61,对应十进制的 97,对应ASCII字符a

 

https://blog.csdn.net/cybersnow/article/details/88079026

C# 代码:

// 16进制字符串转字节数组   格式为 string sendMessage = "00 01 00 00 00 06 FF 05 00 64 00 00";
        private static byte[] HexStrTobyte(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).Trim(), 16);
            return returnBytes;
        }


        // 字节数组转16进制字符串   
        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");//ToString("X2") 为C#中的字符串格式控制符
                }
            }
            return returnStr;
        }
        //字节数组转16进制更简单的,利用BitConverter.ToString方法
        //string str0x = BitConverter.ToString(result, 0, result.Length).Replace("-"," ");
————————————————
版权声明:本文为CSDN博主「未来之窗软件服务」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cybersnow/article/details/88079026

 

 

data = sock.recv(1024)
# 打印响应消息
response_hex = binascii.hexlify(data).decode("utf-8")
print(f"响应消息:{response_hex}")

 

REF

https://www.zhihu.com/question/593657079/answer/2969143567?utm_id=0

https://pythonjishu.com/lqxiwocfinbnaai/  (good)

https://blog.51cto.com/u_12205/6558661

https://blog.csdn.net/weixin_35757191/article/details/129445416

https://www.python100.com/html/C53VA67J79CT.html

https://www.zhblog.net/qa/socket-in-python.html

https://www.zhihu.com/question/593657079/answer/2969143567?utm_id=0

https://www.zhihu.com/question/593657079/answer/2969143567?utm_id=0