aboutsummaryrefslogtreecommitdiff
path: root/tools/file2json.py
blob: 65547e72e4e8b84ac7af7cdcdc6d00930261fbb0 (plain)
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
'''
Converts a binary file into JSON.

This lets you transform a binary file into something you can
easily bundle inside a web page.

Usage: file2json.py FILENAME VARNAME

FILENAME - the binary file
VARNAME - the variable to store it in (the output will be VARNAME = [...])
'''

import os, sys

data = open(sys.argv[1], 'r').read()
sdata = map(lambda x: str(ord(x)) + ',', data)
sdata[-1] = sdata[-1].replace(',', '')
lined = []
while len(sdata) > 0:
  lined += sdata[:30]
  sdata = sdata[30:]
  if len(sdata) > 0:
    lined += ['\n']
json = '[' + ''.join(lined) + ']'
print json