Python控制台默认十六进制显示

发布于 2024-11-14 18:37:35 字数 302 浏览 8 评论 0原文

我在 Python 控制台中做了很多工作,其中大部分都涉及地址,我更喜欢以十六进制形式查看地址。

因此,如果a = 0xBADF00D,当我只需输入Python>时; a 进入控制台查看其值,我更喜欢 python 回复 0xBADF00D 而不是 195948557

我知道我可以输入 '0x%X' % a 以十六进制形式查看它,但我正在寻找某种 python 控制台选项以使其自动执行此操作。存在这样的东西吗?谢谢!

I'm doing a bunch of work in the Python console, and most of it is referring to addresses, which I'd prefer to see in hex.

So if a = 0xBADF00D, when I simply enter Python> a into the console to view its value, I'd prefer python to reply with 0xBADF00D instead of 195948557.

I know I can enter '0x%X' % a to see it in hex, but I'm looking for some sort of python console option to have it do this automatically. Does something liket this exist? Thanks!

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

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

发布评论

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

评论(5

话少情深 2024-11-21 18:37:35

常规 Python 解释器将调用 sys.displayhook 来实际显示您输入的表达式。您可以将其替换为能够准确显示您想要的内容的内容,但您必须记住,它是为交互式解释器想要显示的所有表达式而调用的:

>>> import sys
>>> 1
1
>>> "1"
'1'
>>> def display_as_hex(item):
...     if isinstance(item, (int, long)):
...         print hex(item)
...     else:
...         print repr(item)
...
>>> sys.displayhook = display_as_hex
>>> 1
0x1
>>> "1"
'1'

我怀疑您很快就会厌倦不过,将所有整数视为十六进制,并相应地切换到显式转换您想要查看的整数。

The regular Python interpreter will call sys.displayhook to do the actual displaying of expressions you enter. You can replace it with something that displays exactly what you want, but you have to keep in mind that it is called for all expressions the interactive interpreter wants to display:

>>> import sys
>>> 1
1
>>> "1"
'1'
>>> def display_as_hex(item):
...     if isinstance(item, (int, long)):
...         print hex(item)
...     else:
...         print repr(item)
...
>>> sys.displayhook = display_as_hex
>>> 1
0x1
>>> "1"
'1'

I suspect you'll quickly get tired of seeing all integers as hex, though, and switch to explicitly converting the ones you want to see as hex accordingly.

彩虹直至黑白 2024-11-21 18:37:35

基于之前的答案,这里有一个适用于 Python 2/3 的版本,不将布尔值显示为十六进制,并且还正确设置了 _ 变量:

import sys

def _displayhook(o):
    if type(o).__name__ in ('int', 'long'):
        print(hex(o))
        __builtins__._ = o
    else:
        sys.__displayhook__(o)

def hexon():
    sys.displayhook = _displayhook
def hexoff():
    sys.displayhook=sys.__displayhook__

Building on previous answers, here's a version that works for Python 2/3, doesn't display bools as hex, and also properly sets the _ variable:

import sys

def _displayhook(o):
    if type(o).__name__ in ('int', 'long'):
        print(hex(o))
        __builtins__._ = o
    else:
        sys.__displayhook__(o)

def hexon():
    sys.displayhook = _displayhook
def hexoff():
    sys.displayhook=sys.__displayhook__
南风几经秋 2024-11-21 18:37:35

也许是这样的?

class HexInt(int):
    "Same as int, but __repr__() uses hex"

    def __repr__(self):
        return hex(self)

因此,您可以在创建要显示为十六进制值的所有整数时使用它。

示例:

>>> a = HexInt(12345)
>>> b = HexInt(54321)
>>> a
0x3039
>>> b
0xd431
>>> c = HexInt(a + b)
>>> c
0x1046a

请注意,如果您想在进行算术运算时跳过显式创建新的 HexInt,则必须重写方法的现有 int 版本,例如 < code>__add__()、__sub__() 等,这样它们就会返回 HexInt

Something like this, perhaps?

class HexInt(int):
    "Same as int, but __repr__() uses hex"

    def __repr__(self):
        return hex(self)

So you'd use that when creating all your integers that you want to be shown as hex values.

Example:

>>> a = HexInt(12345)
>>> b = HexInt(54321)
>>> a
0x3039
>>> b
0xd431
>>> c = HexInt(a + b)
>>> c
0x1046a

Note that if you wanted to skip the explicit creation of a new HexInt when doing arithmetic operations, you'd have to override the existing int versions of methods such as __add__(), __sub__(), etc., such that they'd return HexInts.

叶落知秋 2024-11-21 18:37:35

修改 python3 的顶级 python2 答案...

def display_as_hex(item):
    if isinstance(item, int):
        print(hex(item))
    else:
        print(repr(item))
import sys
sys.displayhook = display_as_hex

Modifying the top python2 answer for python3...

def display_as_hex(item):
    if isinstance(item, int):
        print(hex(item))
    else:
        print(repr(item))
import sys
sys.displayhook = display_as_hex
情深缘浅 2024-11-21 18:37:35

您可以这样做:

while True:
  print hex(input('> '))

获得打印所有结果的十六进制值的基本提示。您甚至可以将其设置为有条件的 - 检查输入的返回类型是否是字符串或数字,如果是,则打印十六进制值,否则正常打印该值。

You could so something like this:

while True:
  print hex(input('> '))

To get a basic prompt that prints the hex value of all of the results. You could even make it conditional -- check to see if the return type of input is a string or number, and if it is, print the hex value, else print the value normally.

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