added some conversion functions to play with

This commit is contained in:
Daplie 2017-01-18 16:49:17 -07:00
parent 91bb98fe2c
commit 7a2fc325ed
2 changed files with 82 additions and 4 deletions

View File

@ -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 {

69
parse-binary-test.js Normal file
View File

@ -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);