在Python中将希伯来字符串转换为Hex

时间:2014-05-21 09:37:51

标签: python unicode hex hebrew

我有这个代码在希伯来语中输入一些字符串并输出字符串的Hex值。

到目前为止,这是我的代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*- 

def str_to_hex(string):
    hex_str = ""
    '... some code ...'
    return hex_str

my_input = raw_input("Enter string: ")
hex_value = str_to_hex(my_input)
print "Your String was: %s\t Hex Value:%s" % (my_input,hex_value)
print result.encode('utf-8')

有什么建议吗? 感谢

2 个答案:

答案 0 :(得分:2)

您可以将encode()用于' hex'参数:

>>> 'blahblah'.encode('hex')
'626c6168626c6168'

你的职能变成:

def str_to_hex(string):
    return string.encode('hex')

答案 1 :(得分:2)

以下是使用binascii.hexlify

的工作代码
#!/usr/bin/env python
# -*- coding: utf-8 -*- 
import binascii


my_input = raw_input("Enter string: ")
hex_value = binascii.hexlify(my_input)
print "Your String was: %s\t Hex Value:%s" % (my_input,hex_value)
print hex_value
相关问题