Skip to content

Instantly share code, notes, and snippets.

@migurski
Last active December 10, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save migurski/4416013 to your computer and use it in GitHub Desktop.
Save migurski/4416013 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
''' Convert binary data to UTF8.
If you want binary data in Javascript over XMLHttpRequest but don't
have ArrayBuffer available, one option is to read strings of text and
use xhr.responseText.charCodeAt() to read them. Because this expects to
work with text, it will return the unicode of the character at that
position. This script converts raw binary bytes to unicode text that
evaluates to the same 8-bit unsigned int at each position. The overall
length will increase slightly.
'''
from struct import pack, unpack
from sys import stdin, stderr, stdout
from optparse import OptionParser
parser = OptionParser(usage='%(prog)s <input> <output>')
defaults = dict(order='little', verbose=False)
parser.set_defaults(**defaults)
parser.add_option('-o', '--order', choices=('little', 'big'),
dest='order', help='Byte order (endianness), default %(order)s.' % defaults)
parser.add_option('-v', '--verbose', action='store_true',
dest='verbose', help='Optionally output size report.')
def byte2uint16_little(byte):
return pack('<H', ord(byte))
def byte2uint16_big(byte):
return pack('>H', ord(byte))
if __name__ == '__main__':
opts, (input, output) = parser.parse_args()
bytes = open(input).read()
if opts.order == 'little':
utf16 = ''.join(map(byte2uint16_little, bytes))
utf8 = utf16.decode('utf-16-le').encode('utf-8')
else:
utf16 = ''.join(map(byte2uint16_big, bytes))
utf8 = utf16.decode('utf-16-be').encode('utf-8')
if opts.verbose:
print >> stderr, 'Converted', len(bytes), 'bytes binary to', len(utf8), 'bytes utf-8'
out = open(output, 'w')
out.write(utf8)
out.close()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>floats.html</title>
</head>
<body>
<script type="application/javascript">
<!--
var x = new XMLHttpRequest(),
t = undefined;
x.onreadystatechange = function()
{
if(x.readyState != 4)
{
return;
}
var ab = new ArrayBuffer(x.responseText.length),
u = new Uint8Array(ab), f = new Float32Array(ab);
console.log(u.length, f.length);
for(var i = 0; i < u.length; i+=1)
{
u[i] = x.responseText.charCodeAt(i);
}
console.log(f[0], f[1], f[2], f[3]);
}
x.open('GET', 'floats.utf8', true);
x.send();
//-->
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment