You should loop through the string and do this on each character:
let result = ""
for (let i = 0; i < myString.length; i++) {
result += myString[i].charCodeAt(0).toString(2) + " ";
}
Answer from nedoder on Stack OverflowThis question has some helpful info: How to do Base64 encoding in node.js?
The Buffer class itself does the conversion:
var base64data = Buffer.from('some binary data', 'binary').toString('base64');
console.log(base64data);
// -> 'c29tZSBiaW5hcnkgZGF0YQ=='
var originaldata = Buffer.from(base64data, 'base64');
console.log(originaldata);
// -> <Buffer 73 6f 6d 65 20 62 69 6e 61 72 79 20 64 61 74 61>
console.log(originaldata.toString());
// -> some binary data
new Buffer is deprecated
From Binary to base64:
const base64data = Buffer.from('some binary data', 'binary').toString('base64');
console.log(base64data);
// -> 'c29tZSBiaW5hcnkgZGF0YQ=='
From base64 to binary:
const origionalData = Buffer.from('some base64 data', 'base64')
console.log(origionalData)
// -> 'c29tZSBiaW5hcnkgZGF0YQ=='
How about this?
var responseData = Buffer.from(x, 'utf8');
from: Convert string to buffer Node
Without digging very deep into your code, it seems to me that you might want to change
var responseData=x.toString();
to
var responseData=x.toString("binary");
and finally
response.write(new Buffer(toTransmit, "binary"));
» npm install binstring
» npm install binary-string
const buffer = new Buffer(binaryString, "binary");
Tested with:
$ node
> var binaryString = "\xff\xfa\xc3\x4e";
> var buffer = new Buffer(binaryString, "binary");
> console.log(buffer);
<Buffer ff fa c3 4e>
Update: since v10.0.0 - Use Buffer.from(string[, encoding]) instead.
const bin = 'PK\u0003\u0004\n\u0000\u0000\u0000\b\u0000\u0013{yB½g].9\u0001\u0000\u00005\u0004\u0000\u0000\u0013\u0000\u0000\u0000[Content_Types].xml'
const buf = new Buffer.from(bin, 'binary');
console.log(buf)
// <Buffer 50 4b 03 04 0a 00 00 00 08 00 13 7b 79 42 bd 67 5d 2e 39 01 00 00 35 04 00 00 13 00 00 00 5b 43 6f 6e 74 65 6e 74 5f 54 79 70 65 73 5d 2e 78 6d 6c>
You probably want to look at pkg
It somehow creates a self-contained binary executable from Javascript, including module dependencies and asset files.
Installation and use is easy:
$ npm install -g pkg
$ pkg index.js -o my-program
$ ./my-program
My understanding is that this binary contains nodejs bytecode. It also appears that you can cross-compile.
Note: I've tried ncc and nexe also, but I haven't found them to be as useful. ncc just creates a self-contained Javascript file and nexe encountered a Python error when I tried to use it.
Solved the same thing using Bun. I added an entry to script in package.json, like this:
"scripts": {
"compile": "bun build src/main.ts --compile --outfile out"
I now run it like bun run compile and i can use the out file like ./out
What you should do is convert every char using charCodeAt function to get the Ascii Code in decimal. Then you can convert it to Binary value using toString(2):
function convert() {
var output = document.getElementById("ti2");
var input = document.getElementById("ti1").value;
output.value = "";
for (var i = 0; i < input.length; i++) {
output.value += input[i].charCodeAt(0).toString(2) + " ";
}
}
<input id="ti1" value ="TEST"/>
<input id="ti2"/>
<button onClick="convert();">Convert!</button>
And here's a fiddle: http://jsfiddle.net/fA24Y/1/
This might be the simplest you can get:
function text2Binary(text) {
return text.split('').map(function (char) {
return char.charCodeAt(0).toString(2);
}).join(' ');
}
Update — using arrow functions:
function text2Binary(text) {
return text.split('').map((char) => char.charCodeAt(0).toString(2)).join(' ');
}