zoukankan      html  css  js  c++  java
  • Convert string to hex « Python recipes « ActiveState Code

    Convert string to hex « Python recipes « ActiveState Code

        Convert string to hex (Python recipe) by Mykola Kharechko
        ActiveState Code (http://code.activestate.com/recipes/496969/)
        2
           

        Qoute string converting each char to hex repr and back
        Python, 14 lines
        Download
        Copy to clipboard

         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14

           

        #convert string to hex
        def toHex(s):
            lst = []
            for ch in s:
                hv = hex(ord(ch)).replace('0x', '')
                if len(hv) == 1:
                    hv = '0'+hv
                lst.append(hv)
           
            return reduce(lambda x,y:x+y, lst)

        #convert hex repr to string
        def toStr(s):
            return s and chr(atoi(s[:2], base=16)) + toStr(s[2:]) or ''

        Share
        Tags: web
        6 comments
        sasa sasa 6 years, 4 months ago  # | flag

        list comprehensions. Though your snippet is nice, you should really have a look at least comprehensions. With a few built-in methods you can reduce your 8 lines of code (which is pretty slow because of the "extra work" I guess) to one:

        toHex = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x])

        The builtin string-method "join" joins every element of the list to the string by re-using the string. ";;".join(['a', 'b', 'c']) would result in 'a;;b;;c'. Note that you can enhance the speed of the above snippet by replacing the [] with () what will change it from a list comprehension to a generator. Same enhancements can be done with your second function.

        Regards, Stargaming
        tomer filiba 6 years, 4 months ago  # | flag

        encode and decode. this functionality already exists with the encodings library (which is built-in):

        >>> "hello".encode("hex")
        '68656c6c6f'
        >>> "68656c6c6f".decode("hex")
        'hello'
        >>>

        Adam Monsen 5 years, 8 months ago  # | flag

        base 16 int conversion. The hex() and int() builtins should do everything you need...

        >>> int('0x10AFCC', 16)
        1093580
        >>> hex(1093580)
        '0x10afcc'

        Adam Monsen 5 years, 8 months ago  # | flag

        Nevermind, I should've read the original post more closely. As the other commenter said, str.encode() and str.decode() do the same thing as your code.
        a 2 years, 7 months ago  # | flag

        atoi() is deprecated. you should use int() instead
        sic_uk 2 years, 5 months ago  # | flag

        Even though modern Python implementations have .encode and .decode, old versions (pre v2) don't.

        My current Python platform (Telit GC864) runs on v1.5.2, with absolutely no option to upgrade.

        Thanks OP!
  • 相关阅读:
    OPENCV图像变换-1
    OPENCV形态学操作1
    OPENCV基本滤波算法
    OSX下编译安装opencv3.1.0与opencv_contrib_master
    iOS8学习笔记-构建多视图应用程序
    iOS8学习笔记2--autolayout
    iOS学习笔记1--在xcode6以上的版本中不使用storyboard以及部分控件使用
    Objective-c学习笔记3
    objective-c学习笔记2
    objective-c学习笔记
  • 原文地址:https://www.cnblogs.com/lexus/p/2826938.html
Copyright © 2011-2022 走看看