如何在Python中将单个字符转换为其十六进制ASCII值? [英] How do I convert a single character into its hex ASCII value in Python?
本文介绍了如何在Python中将单个字符转换为其十六进制ASCII值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有兴趣录入一个字符.
I am interested in taking in a single character.
c = 'c' # for example
hex_val_string = char_to_hex_string(c)
print hex_val_string
输出:
63
最简单的方法是什么?有预定义的字符串库的东西吗?
What is the simplest way of going about this? Any predefined string library stuff?
推荐答案
有几种方法可以做到这一点:
There are several ways of doing this:
>>> hex(ord("c"))
'0x63'
>>> format(ord("c"), "x")
'63'
>>> import codecs
>>> codecs.encode(b"c", "hex")
b'63'
在Python 2上,您还可以使用 hex
编码像这样(不适用于Python 3 +):
On Python 2, you can also use the hex
encoding like this (doesn't work on Python 3+):
>>> "c".encode("hex")
'63'
这篇关于如何在Python中将单个字符转换为其十六进制ASCII值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文