首页 \ 问答 \ 使用Python通过TCP接收的字符串中的非二进制(十六进制)字符(Non-binary(hex) characters in string received over TCP with Python)

使用Python通过TCP接收的字符串中的非二进制(十六进制)字符(Non-binary(hex) characters in string received over TCP with Python)

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

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

什么是\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.


原文:https://stackoverflow.com/questions/3906903
更新时间:2021-06-07 06: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 '

相关问答

更多

将整数拆分为两个连接的十六进制字符串 - Python(Split integer into two concatenated hex strings- Python)

使用divmod内置函数: >>> [hex(x) for x in divmod(70000, 65536)] ['0x1', '0x1170'] Use divmod builtin function: >>> [hex(x) for x in divmod(70000, 65536)] ['0x1', '0x1170']

在python中,如何将十六进制ascii字符串转换为原始的内部二进制字符串?(In python, how to convert a hex ascii string to raw internal binary string?)

import base64 data = base64.b16decode("01000001B8000102030405") import base64 data = base64.b16decode("01000001B8000102030405")

在python 3.5中将字符串编码和解码为二进制或十六进制(encoding and decoding a string to binary or hex in python 3.5)

除了事实,你不应该存储密码, base64模块完全符合你的要求。 这是一个例子: from base64 import b64encode, b64decode mypw = "mypassword".encode() # needs to be a binary-string encrPW = b64encode(mypw) # equals b'bXlwYXNzd29yZA==' assert mypw == b64decode(encrPW) # decoding ...

Python - 二进制转义序列的字符串(Python - string of binary escape sequences as literal)

repr >>> a='\x05\x03\x87' >>> print a ? >>> print repr(a) '\x05\x03\x87' 编辑 Sven指出OP可能希望每个字符都以十六进制转储,甚至是可打印的,在这种情况下,我能想到的最佳解决方案是: >>> print ''.join(map(lambda c:'\\x%02x'%c, map(ord, a))) \x05\x03\x87 其他编辑 四年后,我发现这可能既快又可读: >>> print ''.join(map(lamb ...

从文件读入的Python十六进制字符串不再解释为十六进制(Python hex string read in from file no longer interpreted as hex)

在Python代码中, '\x01'是一个转义序列,在文本文件中它只是文本。 你必须在发送之前解除字符串。 import re def unescape(text): return re.sub(r'\\x([0-9a-fA-F]{2})', lambda g: chr(int(g.group(1),16)), text) In Python code '\x01' is an escape sequence, in a text file it is only text. You h ...

将字符串转换为十六进制并在python中计算二进制结果[关闭](Converting a String to hex and calculate binary result in python [closed])

这实际上非常简单,翻译非常简单。 你可以继续使用你的位移。 唯一的变化是for循环的语法,并使用ord()从字符中获取整数值。 def decode(data, size, offset=0): value = 0 for ch in data[offset:size]: value <<= 6 value |= ord(ch) - 0x30 return value 在解释器中运行它,我得到5432 : >>> decode("1Dh ...

使用Python通过TCP接收的字符串中的非二进制(十六进制)字符(Non-binary(hex) characters in string received over TCP with Python)

在字符串中查看二进制数据有时会让人感到困惑,特别是如果它们很长,但您总是可以将其转换为更容易阅读的十六进制数据。 >>> 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 ...

Lua:从二进制读取十六进制值(Lua: Read hex values from binary)

看看这里 : 作为最后一个例子,以下程序将转储一个二进制文件。 再一次,第一个程序参数是输入文件名; 输出转到标准输出。 程序以10个字节为单位读取文件。 对于每个块,它会写入每个字节的十六进制表示,然后将块写入文本,将控制字符更改为点。 local f = assert(io.open(arg[1], "rb")) local block = 10 while true do local bytes = f:read(block) if not bytes then break end ...

在Python中删除字符串中的所有十六进制字符(Remove all hex characters from string in Python)

只要删除所有非ASCII字符: >>> s.decode('utf8').encode('ascii', errors='ignore') 'http://www.google.com blah blah#%#@$^blah' 其他解决方案: >>> import string >>> s = '\xe2\x80\x9chttp://www.google.com\xe2\x80\x9d blah blah#%#@$^blah' >>> printable = set(string.printab ...

将二进制字符串转换为十六进制字符串并保持其前导零(Convert binary string to hex string and keep its leading zeros)

只需将位数除以4: >>> bits = "0000000000001010" >>> '{:0{}X}'.format(int(bits, 2), len(bits) // 4) '000A' Just divide the number of bits by 4: >>> bits = "0000000000001010" >>> '{:0{}X}'.format(int(bits, 2), len(bits) // 4) '000A'

相关文章

更多

最新问答

更多
  • 如何从远程文件拉取文件而不覆盖本地文件?(How do I pull files from remote without overwriting local files?)
  • Reactjs:状态改变时重新渲染iframe(Reactjs: re-renders iframes when state changed)
  • 奇怪的网址,以及跟随php页面流程的困难(odd url, and difficulty in following the php page flow)
  • 标签活动无效(Tab Activity is not working)
  • JavaME合适的语法编译器建议?(JavaME-suitable grammar compiler recommendations?)
  • 指定参数(Specifying arguments)
  • 可以通过Ruby插件或控制台覆盖Sketchup中的键盘快捷键吗?(Can one override keyboard shortcuts in Sketchup through the a Ruby Plugin or Console?)
  • 计算Java EE Web App中用户数的最佳方法(Best way to count number of users in a Java EE web App)
  • 无法使用templateUrl加载cordova中的外部模板(unable to load external templates in cordova with templateUrl)
  • PHPExcel:写入期间无法使用缓存(PHPExcel: Unable to use cache during write)
  • 在javascript中嵌套这个指针(nested this pointer in javascript)
  • 谁跟领航致远培训过,有问题问下啊
  • 控制器要求在入门时下载(Controller ask to download on entry)
  • 未能通过conda安装Asyncio(Failure to install Asyncio via conda)
  • 如何查找已完成项目的总长度?(How to find length of total completed items?)
  • 如何检查OleInitialize是否已被调用?(How to check if OleInitialize has already been called?)
  • SQL在特定范围内返回列中具有最大值的行(SQL Returning rows with max value in column, within a specific range)
  • preg_match从url获取id(preg_match get the id from url)
  • 如何在运算符中为make方程转换perl变量?(How to convert a perl variable in a operator for make equations?)
  • 在导航上方添加空格/标题。(Add a white space/ header above navigation.)
  • MeetingItem已保存;(MeetingItem saved; but change now shown in Calendar)
  • c#vb:我们应该使用System.Lazy进行资源密集型任务吗?(c# vb: Should we use System.Lazy for resource-intensive task? (when threading is not needed))
  • 为什么在armeabi代码中使用armeabi-v7a代码?(Why use armeabi-v7a code over armeabi code?)
  • 获取请求的自定义标头(Java HTTP)(Fetching a custom header of a request (Java HTTP))
  • 是否可以在嵌套的if语句中从varchar转换为numeric以动态评估参数?(Is it possible to convert from varchar to numeric within a nested if statement in order to dynamically evaluate a parameter?)
  • 如何将Html.ActionLink转换为链接到Ajax调用的按钮?(How to convert from Html.ActionLink to a button linked to Ajax call?)
  • 应用程序如何处理Windows符号链接?(How are Windows symbolic links treated by the apps?)
  • html,js,css在jsfiddle中工作,但不在sharepoint中(html, js, css works in jsfiddle but not in sharepoint)
  • 从Ruby脚本调用Elasticsearch Rest API(Calling Elasticsearch Rest API from Ruby script)
  • 如何将嵌套setTimeouts转换为承诺(How to convert nested setTimeouts to promises)