fix binaryString conversion

This commit is contained in:
AJ ONeal 2015-06-10 19:28:03 -06:00
parent 10967b0a55
commit 787dc230c2
1 changed files with 1 additions and 43 deletions

View File

@ -51,7 +51,7 @@ function base64ToUtf8(b64) {
function bufferToBinaryString(buf) { function bufferToBinaryString(buf) {
var binstr = Array.prototype.map.call(buf, function (ch) { var binstr = Array.prototype.map.call(buf, function (ch) {
return '0x' + String.fromCharCode(ch); return String.fromCharCode(ch);
}).join(''); }).join('');
return binstr; return binstr;
@ -84,48 +84,6 @@ function base64ToBuffer(base64) {
return buf; return buf;
} }
// Hex Convenience Functions
, hexToBuffer: hexToBuffer
, bufferToHex: bufferToHex
function bufferToHex(arr) {
var i;
var len;
var hex = '';
var c;
for (i = 0, len = arr.length; i < len; i += 1) {
c = arr[i].toString(16);
if (c.length < 2) {
c = '0' + c;
}
hex += c;
}
return hex;
}
function hexToBuffer(hex) {
// TODO use Uint8Array or ArrayBuffer or DataView
var i;
var byteLen = hex.length / 2;
var arr;
var j = 0;
if (byteLen !== parseInt(byteLen, 10)) {
throw new Error("Invalid hex length '" + hex.length + "'");
}
arr = new Uint8Array(byteLen);
for (i = 0; i < byteLen; i += 1) {
arr[i] = parseInt(hex[j] + hex[j + 1], 16);
j += 2;
}
return arr;
}
window.Unibabel = { window.Unibabel = {
utf8ToBinaryString: utf8ToBinaryString utf8ToBinaryString: utf8ToBinaryString
, utf8ToBuffer: utf8ToBuffer , utf8ToBuffer: utf8ToBuffer