forked from notmyname/python_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompress_hex_string.py
More file actions
38 lines (30 loc) · 819 Bytes
/
compress_hex_string.py
File metadata and controls
38 lines (30 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python2.4
hex_values = ['%X'%x for x in range(16)]
hex_data = ''.join(hex_values) * 16 * 256
len_hex_data = len(hex_data)
def compress_hex_str(data):
res = []
for i in range(0,len(data),2):
a = int(data[i],16)
b = int(data[i+1],16)
blob = (a << 4L) | b
res.append(chr(blob))
return ''.join(res)
def expand_hex_str(data):
res = []
for i in range(len(data)):
blob = ord(data[i])
top = blob >> 4L
bottom = blob & 15L
res.append('%X'%top)
res.append('%X'%bottom)
return ''.join(res)
c_data = compress_hex_str(hex_data)
len_c_data = len(c_data)
print len_hex_data
print len_c_data
#print hex_data
#print `c_data`
#print expand_hex_str(c_data)
print '%.4f%% compression' % (100.0 - 100.0*float(len_c_data)/float(len_hex_data))
assert hex_data == expand_hex_str(c_data)