如何简单地打印python“\xaa\xbb\xcc\xdd”作为0xddccbbaa? [英] How to simply have python print "\xaa\xbb\xcc\xdd" as 0xddccbbaa?
问题描述
我相信我误解了一些可能很简单的事情。
我试图接受传递给python脚本的参数。我期待的参数类型是沿着\xaa\xbb\xcc\xdd
行的某处,并且它会一直将其转换为二进制我想?),而不是实际上允许我打印出它是如何传递的。
我怎样才能做到我想要做的事? ?我最终希望能够将它转换为像 0xddccbbaa
之类的东西,但我想我至少希望通过能够完成第一步来解释它。
我不想打印 \x75
来打印出一个û
。我希望能够将 \x75
解释为 \x75
。任何简单的方法来做到这一点?
一些示例与 struct
:
x = b\xaa\xbb\xcc\xdd
导入结构体
struct.unpack('I', x)
Out [3]:(3721182122,)
y = struct.unpack('I',x)
y [0]
Out [5]:3721182122
hex(y [0])
Out [6]:'0xddccbbaa'
基本上:将字节串视为小端4字节无符号整数('I'
)。 struct
把它变成 int
,你可以使用 hex
以十六进制格式获取它的字符串表示形式(或者使用类似于'{:x}'。format(y [0])
,如果您愿意的话) p>
I believe I'm misunderstanding something that should probably be simple.
I'm trying to accept arguments passed to a python script. The type of argument I'm expecting is somewhere along the lines of "\xaa\xbb\xcc\xdd"
, and it keeps converting it to binary(i think?) instead of actually allowing me to print it out just how it was passed.
How can I get this to do what I'm looking for it to do? I'd ultimately like to take this and be able to convert it to something like 0xddccbbaa
, but I guess I'd at least like to get the first step completed by being able to interpret it.
Like, I don't want printing \x75
to print out a u
. I want to be able to interpret \x75
as \x75
. Any easy way to do this?
Some demonstrations with struct
:
x = b"\xaa\xbb\xcc\xdd"
import struct
struct.unpack('I',x)
Out[3]: (3721182122,)
y = struct.unpack('I',x)
y[0]
Out[5]: 3721182122
hex(y[0])
Out[6]: '0xddccbbaa'
Essentially: treat the bytestring as a little-endian 4-byte unsigned integer ('I'
). struct
handles turning it into an int
, and you can use hex
to get a string representation of it in hex (or use something like '{:x}'.format(y[0])
, if you prefer)
这篇关于如何简单地打印python“\xaa\xbb\xcc\xdd”作为0xddccbbaa?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!