【问题标题】:Convert hebrew strings to Hex in Python在 Python 中将希伯来语字符串转换为十六进制
【发布时间】:2014-05-21 09:37:50
【问题描述】:

我有这段代码,它以希伯来语输入一些字符串并输出字符串的十六进制值。

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

#!/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')

有什么建议吗? 谢谢

【问题讨论】:

    标签: python unicode hex hebrew


    【解决方案1】:

    这是使用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
    

    【讨论】:

      【解决方案2】:

      您可以将encode() 与“hex”参数一起使用:

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

      你的函数变成:

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

      【讨论】:

        猜你喜欢
        • 2011-06-09
        • 2011-11-22
        • 2023-03-19
        • 1970-01-01
        • 2019-02-10
        • 2014-03-07
        • 1970-01-01
        • 2020-06-20
        • 2015-01-06
        相关资源
        最近更新 更多