python 代码收集

1,本地时间字符串,转格林威治时间字符串

def localToGtime(s):
    timeArray = time.strptime(s, "%Y%m%d%H%M%S")
    timeStamp = int(time.mktime(timeArray))
    gmtimes = time.strftime("%Y%m%d%H%M%S", time.gmtime(timeStamp))

    return gmtimes

2. 十六进制字符串,转为二进制字符串。思路是16进制字符串转十进制,在转二进制

def dec2bin(num):
    base = [0, 1]
    # num = int(string_num)
    mid = []
    while True:
        if num == 0: break
        num, rem = divmod(num, 2)
        # print(num,rem)
        mid.append(base[rem])

    return ''.join([str(x) for x in mid[::-1]])

def hextobin(msg):
    r = ''
    for m in msg:
        res = int(m, 16)
        s = str(dec2bin(res))
        r1 = ''
        for i in range(0, 4 - len(s)):
            r1 = '0' + r1
        r1 = r1 + s
        r = r + r1
    return r

 

你可能感兴趣的:(python学习)