Simplify binary to string Javascript possible?

Searching for a way to simplify this:

let testbin = '01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01100100'

alert(bin2text(testbin))

function bin2text(bin) {
  bin = bin
    .split(" ");
  return bin.map(elem => String.fromCharCode(parseInt(elem, 2)))
    .join("");
}

Is it possible using vanilla JS?

That IS vanilla JS.

Yes, but I do not want anybody be temptated to simplify using JQuery :slight_smile:

What I am looking for - but not found - is something like this

let string = bin2text(testbin) (a standard command)

huh? I’m obviously missing something here…

You’ve essentially already doing that, except you’re alerting the value instead of assigning it to another variable. bin2text is already a method passing in a string and returning a mapped value.

Here - I’ve assigned it to a variable before it’s alerted.

let testbin = '01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01100100';

let someString = bin2text(testbin);

alert(someString);

function bin2text(bin) {
  bin = bin
    .split(" ");
  return bin.map(elem => String.fromCharCode(parseInt(elem, 2)))
    .join("");
}

In my dreams I want this to be way simpler - like this:

let testbin = '01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01100100';
let someString = bin2text(testbin);
alert(someString);

No extra function. Possible?

As your string has nothing to do with a binary value why should this be possible? Converting a string of any type to something else needs some code.

That’s not a built in javascript method.

Just for fun

function bin2text(binary) {
  return binary.replaceAll(/(\d+)\s?/g, (a,b) => String.fromCharCode(parseInt(b, 2)))
}

To be fair, the extra function is a one-liner function (even if you’re splitting it up…), so you could do it “No extra function”, it’d just be a slightly longer line.

Also, fromCharCode can take a splattered array. So…

String.fromCharCode(...binary.split(' ').map(x => parseInt(x, 2)));
3 Likes

Well, yeah. But there is something to be said for readability (or if it’s used more than that one singular place…)

1 Like

Let’s simplify the original code by extracting out bin2num() and bin2char() functions.

const bin2num = (bin) => parseInt(bin, 2);
const bin2char = (bin) => String.fromCharCode(bin2num(bin));

let testbin = "01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01100100"
alert(bins2text(testbin));

function bins2text(bins) {
  bin = bins
    .split(" ");
  return bin.map(bin2char)
    .join("");
}

Simplifying things a bit further we end up with:

const bin2num = (bin) => parseInt(bin, 2);
const bin2char = (bin) => String.fromCharCode(bin2num(bin));
const bin2text = (bin) => bin.split(" ").map(bin2char).join("");

let testbin = "01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01100100"
alert(bin2text(testbin));

Each part of that is nice and simple now.

  • bin2num takes a binary string and converts it into a single number
  • bin2char takes a binary string and creates a character
  • bin2text takes multiple binary strings and converts them to characters
1 Like

I’m not sure OP would consider 3 functions simpler than one, considering they balked at Dave’s original answer with 1… :wink:

1 Like

That’s the general principle with simpler code, it expands the size of the code.

Frequently people try to make code smaller by making it more complex, with the extreme version of that being code golf where you attempt to do vast amounts of things in the smallest number of characters.

The other side of that complex coin is making things simpler, which requires more lines of code in order to achieve a simplification of the code.

If the OP is instead wanting a built-in JavaScript function that natively does the conversion, then he’s barking up the wrong tree for that doesn’t exist.

1 Like

Well I dont know that everyone would agree with your definition of simpler, is all i’m saying.

If there’s 40 functions to bounce around through to figure out what 1 line of code does, I dont consider that simpler. shrug To each their own. The OP now has several ways of accomplishing the goal, which is always a good thing… choices. :slight_smile:

2 Likes

If we’re not going to use those smaller functions elsewhere, they can be inside of the bin2text() function instead.

function bin2text(bin) {
    const bin2num = (bin) => parseInt(bin, 2);
    const bin2char = (bin) => String.fromCharCode(bin2num(bin));
    return bin.split(" ").map(bin2char).join("");
}

2 Likes

Thank you all! The above is what all are saying. There are only different ways to skin the cat. More or less readable.

function bin2text(bin) {
  return (String.fromCharCode(...bin.split(' ').map(bin => parseInt(bin, 2))));
}

Again thank you for your time convincing me…

1 Like