使用 Python 通过 TCP 接收的字符串中的非二进制(十六进制)字符

发布于 2024-09-27 07:07:06 字数 246 浏览 0 评论 0原文

也许这是一个菜鸟问题,但我正在通过 TCP 接收一些数据,当我查看字符串时,我得到以下内容:

\x00\r\xeb\x00\x00\x00\x00\x01t\x00< /code>

那个 \r 字符是什么?\x01t 中的 t 是什么意思?

我尝试过谷歌搜索,但我不知道谷歌搜索什么...

谢谢。

maybe this is a noob question, but I'm receiving some data over TCP and when I look at the string I get the following:

\x00\r\xeb\x00\x00\x00\x00\x01t\x00

What is that \r character, and what does the t in \x01t mean?

I've tried Googling, but I'm not sure what to Google for...

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云淡风轻 2024-10-04 07:07:06

\r 是回车符 (0x0d),tt

\r is a carriage return (0x0d), the t is a t.

陈年往事 2024-10-04 07:07:06

查看字符串中的二进制数据有时可能会令人困惑,尤其是当它们很长时,但您始终可以将其转换为更易于阅读的十六进制。

>>> data = '\x00\r\xeb\x00\x00\x00\x00\x01t\x00'
>>> ' '.join(["%02X" % ord(char) for char in data])
'00 0D EB 00 00 00 00 01 74 00'

另外,如果您只是将字节字符串解析为字段,则只需忽略该字符串并直接使用 struct 模块:

>>> import struct
>>> length, command, eggs, spam = struct.unpack('!BBi4s',data)
>>> #...whatever your fields really are
>>> print "len: %i\ncmd: %i\negg qty: %i\nspam flavor: '%s'" % (
...     length, command, eggs, spam)
len: 0
cmd: 13
egg qty: -352321536
spam flavor: ' ☺t '

Viewing binary data in strings can sometimes be confusing, especially if they're long, but you can always convert it to some easier-to-read hex.

>>> data = '\x00\r\xeb\x00\x00\x00\x00\x01t\x00'
>>> ' '.join(["%02X" % ord(char) for char in data])
'00 0D EB 00 00 00 00 01 74 00'

Also, if you're just parsing the byte string into fields, just ignore the string and just go right to unpacking it with the struct module:

>>> import struct
>>> length, command, eggs, spam = struct.unpack('!BBi4s',data)
>>> #...whatever your fields really are
>>> print "len: %i\ncmd: %i\negg qty: %i\nspam flavor: '%s'" % (
...     length, command, eggs, spam)
len: 0
cmd: 13
egg qty: -352321536
spam flavor: ' ☺t '
静赏你的温柔 2024-10-04 07:07:06

将数据显示为字符串时,可打印字符(例如“t”显示为字符,已知控制序列显示为转义符,其他字节以 \x## 形式显示。示例:

>>> s='\x74\x0d\x99'
>>> s
't\r\x99'

您可以使用以下命令转储十六进制形式:

>>> import binascii
>>> binascii.hexlify(s)
'740d99'

When displaying data as a string, printable characters (such as 't' are displayed as characters, known control sequences are displayed as escapes, and other bytes are displayed in \x## form. Example:

>>> s='\x74\x0d\x99'
>>> s
't\r\x99'

You can dump a hexadecimal form with:

>>> import binascii
>>> binascii.hexlify(s)
'740d99'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文