Created
August 28, 2014 17:01
-
-
Save kira924age/3bfeffc9f25b8c400b0f to your computer and use it in GitHub Desktop.
2進数文字列を16進数文字列に変換する。(format(int(hoge, 2), 'x')が使えないとき用)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: utf-8 -*- | |
import sys | |
argv = sys.argv | |
argc = len(argv) | |
if argc != 2: | |
print 'Usage: python bin2hex.py [file_name]' | |
quit() | |
try: | |
infile = open(argv[1], 'r') | |
data = infile.read().replace(' ', '').replace('\n', '').replace('\r', '') | |
except: | |
print 'Error: %s is not found.' % argv[1] | |
quit() | |
outfile = open(raw_input('out file name:'), 'w') | |
c = b = h = 0 | |
results = '' | |
for i in data[::-1]: | |
h += int(i)*2**b | |
c += 1 | |
b += 1 | |
if c == 4: | |
results += format(h, 'x') | |
c = h = b = 0 | |
outfile.write(results[::-1]) | |
infile.close() | |
outfile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment