From 7a2fc325ed233f5ccb956293f470d4f4050a8c72 Mon Sep 17 00:00:00 2001 From: Daplie Date: Wed, 18 Jan 2017 16:49:17 -0700 Subject: [PATCH] added some conversion functions to play with --- listen.js | 17 ++++++++--- parse-binary-test.js | 69 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 parse-binary-test.js diff --git a/listen.js b/listen.js index 2695957..cc12267 100644 --- a/listen.js +++ b/listen.js @@ -17,10 +17,19 @@ function pad(str, len, ch) { while (str.length < len) { str = ch + str; } - return str; } +function binaryAgent(str) { + +var binString = ''; + +str.split(' ').map(function(bin) { + binString += String.fromCharCode(parseInt(bin, 2)); + }); +return binString; +} + socket.on('message', function (message, rinfo) { console.log('Received %d bytes from %s:%d\n', message.length, rinfo.address, rinfo.port); @@ -32,9 +41,9 @@ socket.on('message', function (message, rinfo) { }); - console.log('got here'); - console.log(message.toString('hex')); - console.log(message.toString('ascii')); + + // console.log(message.toString('hex')); + // console.log(message.toString('ascii')); var packets; try { diff --git a/parse-binary-test.js b/parse-binary-test.js new file mode 100644 index 0000000..6b906d0 --- /dev/null +++ b/parse-binary-test.js @@ -0,0 +1,69 @@ +'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); +//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);