比这篇新的文章: Codee#5862
比这篇旧的文章: VBS发送带附件邮件

head.py 输出文件开头的8个字节的二进制表示

语言: Python, 标签: 文件 2009/09/09发布 11个月前更新 更新记录
作者: 半瓶墨水, 点击648次, 评论(0), 收藏者(0), , 打分:

背景
主题: 字体:
01 #! /usr/bin/env python
02 # -*- coding: utf-8 -*-
03 #
04 # Recently I start to write an ico file maker
05 #   this script helps on analyze the file header
06 #
07 # Usage:
08 #   head xxx.ico    - output 16 bytes
09 #   head xxx.ico 32 - output 32 bytes
10 #
11 import sys
12 f = open(sys.argv[1], "rb")
13 L = 16
14 if len(sys.argv) >= 3:
15   L = int(sys.argv[2])
16 bytes = f.read(L)
17 for i in range(0, len(bytes), 8):
18   print bytes[i:i+8]
19
20 xbytes = ["%02x" % ord(b) for b in bytes]
21 print
22 for i in range(0, len(bytes), 8):
23   print "x " + " ".join(xbytes[i:i+8])
24
25 def to_bits(b):
26   bs = []
27   while b:
28     bs.append(b&1)
29     b>>=1
30   bs = map(str, bs)
31   bs.reverse()
32   return "".join(bs).zfill(8)
33
34 bits = [to_bits(ord(b)) for b in bytes]
35 print
36 for i in range(0, len(bytes), 8):
37   print "b " + " ".join(bits[i:i+8])


所有评论,共0条:( 我也来说两句)


发表评论

注册登录后再发表评论