dns-suite.js/parse-binary-test.js

72 lines
1.3 KiB
JavaScript

'use strict';
// This test code is meant to take a binary sample from the crash of listen.js
// example and parse through it to spot where the error occurs.
// ex: pad('11111', 8, '0')
function pad(str, len, ch) {
while (str.length < len) {
str = ch + str;
}
// What is the hex value of the str?
var hex_val = ParseInt(Number(str));
return str;
}
// ex: bin2hex('10010101')
function bin2hex(str){
var hex = parseInt(str, 2).toString(16);
return hex;
}
// ex: hex2ascii('A92F')
function hex2ascii(str){
var hex = str.toString();
var ascii = '';
for (var n = 0; n < hex.length; n+=2) {
ascii += String.fromCharCode(parseInt(hex.substr(n,2), 16));
}
return ascii;
}
function binaryAgent(str) {
var binString = '';
str.split(' ').map(function(bin) {
binString += String.fromCharCode(parseInt(bin, 2));
});
return binString;
}
var str = binaryAgent('01000001 01110010 01100101 01101110 00100111 01110100');
console.log(str);
parse
//translates to "Aren't"
// // binary to hex convestion
// var bin = '00001111';
// var hex = bin2hex(bin);
// // hex to ascii character converstion
// console.log('binary: ' + bin + ' Hex: ' + hex);
// var hex = '';
// var ascii = hex2ascii(hex);
// console.log('Hex: ' + hex + ' ascii: ' + ascii);