python -- 字符十六进制列表


有时候, 需要用到可打印字符的十六进制, 每次都需要处理比较麻烦, 今天写一个小脚本.

gnu@dev:~$ python ./generator_object.py -n 7
('0: 0x30', '1: 0x31', '2: 0x32', '3: 0x33', '4: 0x34', '5: 0x35', '6: 0x36')
('7: 0x37', '8: 0x38', '9: 0x39', 'a: 0x61', 'b: 0x62', 'c: 0x63', 'd: 0x64')
('e: 0x65', 'f: 0x66', 'g: 0x67', 'h: 0x68', 'i: 0x69', 'j: 0x6a', 'k: 0x6b')
('l: 0x6c', 'm: 0x6d', 'n: 0x6e', 'o: 0x6f', 'p: 0x70', 'q: 0x71', 'r: 0x72')
('s: 0x73', 't: 0x74', 'u: 0x75', 'v: 0x76', 'w: 0x77', 'x: 0x78', 'y: 0x79')
('z: 0x7a', 'A: 0x41', 'B: 0x42', 'C: 0x43', 'D: 0x44', 'E: 0x45', 'F: 0x46')
('G: 0x47', 'H: 0x48', 'I: 0x49', 'J: 0x4a', 'K: 0x4b', 'L: 0x4c', 'M: 0x4d')
('N: 0x4e', 'O: 0x4f', 'P: 0x50', 'Q: 0x51', 'R: 0x52', 'S: 0x53', 'T: 0x54')
('U: 0x55', 'V: 0x56', 'W: 0x57', 'X: 0x58', 'Y: 0x59', 'Z: 0x5a', '!: 0x21')
('": 0x22', '#: 0x23', '$: 0x24', '%: 0x25', '&: 0x26', "': 0x27", '(: 0x28')
('): 0x29', '*: 0x2a', '+: 0x2b', ',: 0x2c', '-: 0x2d', '.: 0x2e', '/: 0x2f')
(':: 0x3a', ';: 0x3b', '<: 0x3c', '=: 0x3d', '>: 0x3e', '?: 0x3f', '@: 0x40')
('[: 0x5b', '\\: 0x5c', ']: 0x5d', '^: 0x5e', '_: 0x5f', '`: 0x60', '{: 0x7b')
('|: 0x7c', '}: 0x7d', '~: 0x7e', ' : 0x20', '\t: 0x9', '\n: 0xa', '\r: 0xd')
gnu@dev:~$ python ./generator_object.py -n 8
('0: 0x30', '1: 0x31', '2: 0x32', '3: 0x33', '4: 0x34', '5: 0x35', '6: 0x36', '7: 0x37')
('8: 0x38', '9: 0x39', 'a: 0x61', 'b: 0x62', 'c: 0x63', 'd: 0x64', 'e: 0x65', 'f: 0x66')
('g: 0x67', 'h: 0x68', 'i: 0x69', 'j: 0x6a', 'k: 0x6b', 'l: 0x6c', 'm: 0x6d', 'n: 0x6e')
('o: 0x6f', 'p: 0x70', 'q: 0x71', 'r: 0x72', 's: 0x73', 't: 0x74', 'u: 0x75', 'v: 0x76')
('w: 0x77', 'x: 0x78', 'y: 0x79', 'z: 0x7a', 'A: 0x41', 'B: 0x42', 'C: 0x43', 'D: 0x44')
('E: 0x45', 'F: 0x46', 'G: 0x47', 'H: 0x48', 'I: 0x49', 'J: 0x4a', 'K: 0x4b', 'L: 0x4c')
('M: 0x4d', 'N: 0x4e', 'O: 0x4f', 'P: 0x50', 'Q: 0x51', 'R: 0x52', 'S: 0x53', 'T: 0x54')
('U: 0x55', 'V: 0x56', 'W: 0x57', 'X: 0x58', 'Y: 0x59', 'Z: 0x5a', '!: 0x21', '": 0x22')
('#: 0x23', '$: 0x24', '%: 0x25', '&: 0x26', "': 0x27", '(: 0x28', '): 0x29', '*: 0x2a')
('+: 0x2b', ',: 0x2c', '-: 0x2d', '.: 0x2e', '/: 0x2f', ':: 0x3a', ';: 0x3b', '<: 0x3c')
('=: 0x3d', '>: 0x3e', '?: 0x3f', '@: 0x40', '[: 0x5b', '\\: 0x5c', ']: 0x5d', '^: 0x5e')
('_: 0x5f', '`: 0x60', '{: 0x7b', '|: 0x7c', '}: 0x7d', '~: 0x7e', ' : 0x20', '\t: 0x9')


-------------------------------------------------

#!/usr/bin/env python

import sys
import string
from optparse import OptionParser


def groupby(lst, n):
    return zip(*([iter(lst)] * n))


def gen_func(lst):
    for i in lst:
        str_i = str(i)
        yield "{str}: {hex}".format(str=str_i, hex=hex(ord(str_i)))


def chars_code(n):
    chars = (gen_func(string.printable))

    ret = groupby(list(chars), n)
    for i in ret:
        print i
        # sys.stdout.write(i)


def main():
    parser = OptionParser()
    parser.add_option('-n',
                      '--num',
                      metavar='NUM',
                      help='set a number for group',
                      dest='num'
                      )
    options, args = parser.parse_args()

    if not options.num:
        parser.error('select -h for help')
    else:
        chars_code(int(options.num))


if __name__ == '__main__':
    main()



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值