不使用内置函数,将字符串转换为整形

不使用内置函数,如何将字符串转换为整形

    def convert_to_int(str):
        s = 0
        for i in str:
            s *= 10
            tmp = ord(i) - ord('0')
            s += tmp
        return s


    val = convert_to_int("1234")
    print(val, type(val))    #1234 <class 'int'>

猜你喜欢

转载自blog.csdn.net/qq_32735511/article/details/88397842