如何将单个字符转换为Python中的十六进制ascii值 [英] How do I convert a single character into it's hex ascii value in python

查看:138
本文介绍了如何将单个字符转换为Python中的十六进制ascii值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  c ='c'#例如
hex_val_string = char_to_hex_string(c)
print hex_val_string

输出:

  63 

什么是最简单的这种方式呢?任何预定义的字符串库的东西?

解决方案



 >>> hex(ord(c))
'0x63'
>>>格式(ord(c),x)
'63'
>>> c.encode(hex)
'63'

要使用< Python 3中的code> hex 编码,使用

 >>> codecs.encode(bc,hex)
b'63'


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

output:

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'
>>> "c".encode("hex")
'63'

To use the hex encoding in Python 3, use

>>> codecs.encode(b"c", "hex")
b'63'

这篇关于如何将单个字符转换为Python中的十六进制ascii值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆