I need code

i’m looking for the code to convert binary to text and text to binary and save it to my database. I’m planning to use react.js in the front-end, node.js in the back and Mongodb as a database. I want to convert the password into binary and then save it to database.

For me the question isn’t how but why do you want the password as binary?

1 Like

i’m totally free these days. i have been trying new things. i’m also working on a chat app with socket io and next.js. binary is just a part of it :smiley: :upside_down_face:

If it is just for learning, then that makes sense.

This will only work for a string using the ASCII char set:

const password = "TestPw";

password
    .split('') // make an array from the string ['T', 'e', 's', 't' ,'P' ,'w']
    .map(letter => letter.charCodeAt() // convert the string into the ASCII code(int), e.g. T = 84
                     .toString(2) // Convert the ASCII code into a string, 2 is the radix and converts the int to it's base 2 representation
    ) // map returns an array of all the base 2 representations
    .join(' '); // convert the array into one string 1010100 1100101 1110011 1110100 1010000 1110111

Is that the sort of thing you was after?

i used on my js file, stored binary on a variable and it works fine…thanks!

Glad it worked for you. I didn’t do the binary back to string as I thought you might like to try figure it out, but it is just the reverse logic:

  1. Split the binary string.
  2. Map over the values from the split doing:
  • Convert the binary into the ASCII code (int).
  • Convert the ASCII code into the string value.
  1. Join the array from map back into the plain text representation.

If you get stuck feel free to shout.

You can try this for binary to text:

    var bintext="", textresult="", binlength, text_value="01000001";
    var aaa = text_value;
    bintext = aaa.replace(/[^01]/g, "");
		binlength = bintext.length-(bintext.length%8);
		for(var z=0; z<binlength; z=z+8) {
			textresult += String.fromCharCode(parseInt(bintext.substr(z,8),2));
                            console.log(textresult);
    }
1 Like

One thing to note with this implementation is that it requires the input binary to have had any leading zeros added to it. It will not work with the string to binary implementation I gave above. You’d need to change what I gave to be like so:

const password = "TestPw";

password
    .split('')
    .map(letter => letter.charCodeAt()
                     .toString(2)
                     .padStart(8, '0') // 8 is for the length of the binary (8 bit) and 0 are what gets added to the start if the string is less than 8 long.
    )
    .join(' '); // 01010100 01100101 01110011 01110100 01010000 01110111 could also be join('')
1 Like

man, your updated code returns correct answers for special characters and numbers as well check. Great!

thanks man, i have been looking for this one as well

this is what i did to add zero for the binary less then eight characters, found code somewhere on stackoverfalw:

var text_value = “AA”;
//var ans = document.getElementById(“output”);

    for (var i=0; i < text_value.length; i++)
    {
      var e = text_value[i].charCodeAt(0);
      var s = "";
      do {
        var a = e % 2;
        e = (e - a) / 2;
s = a + s;

}while(e!=0);
while(s.length<8) {
if(s.length === 7) {
s = " 0" + s;
console.log(s);
// this.ans += s;
}
if(s.length === 6) {
s = " 00" + s;
console.log(s);
// ans.value += s;
}
if(s.length === 5) {
s = " 000" + s;
console.log(s);
// ans.value += s;
}
if(s.length === 4) {
s = " 0000" + s;
console.log(s);
// ans.value += s;
}
if(s.length === 3) {
s = " 00000" + s;
console.log(s);
ans.value += s;
}
}
}

That’s because that website is using the code @method0918698232 posted, so needed the leading zeros. This site, for example, is using the sort of implementation that I was suggesting, which will work without leading zeros as there are spaces between each lot of 8 bits and it won’t work with a binary input without spaces.

Really neither is a complete solution as they don’t validate the input.

I’m gonna stick to this code, its simple and short:

const password = "TestPw";

password
    .split('')
    .map(letter => letter.charCodeAt()
                     .toString(2)
                     .padStart(8, '0') // 8 is for the length of the binary (8 bit) and 0 are what gets added to the start if the string is less than 8 long.
    )
    .join(' '); // 01010100 01100101 01110011 01110100 01010000 01110111 could also be join('')

Now, working on the reverse operation.

The do while can be simplified to:

// We don't really need a do here as we are padding after anyway, it is just the difference
// of padding all 8 or just 7 zeros in the worst case.
while( e != 0) { 
    s = (e & 1) + s; // & 1 is like mod 2 for positive numbers
    e >>= 1; // this is like floor divide by 2 for positive numbers
}

If you didn’t want to do padStart you could do "0".repeat(8 - s.length), or if you really want concatenation like you are, a switch would be a little easier to read than lots of s.length == x, maybe:

switch(s.length) {
  case 7:
    s = " 0" + s;
    break;
  case 6:
    s = " 00" + s
    break;
  case 5:
    s = " 000" + s
    break;
  case 4:
    s = " 0000" + s
    break;
  case 3:
    s = " 00000" + s
    break;
  case 2:
    s = " 000000" + s
    break;
  case 1:
    s = " 0000000" + s
    break;
  // not needed currently but if you went for just a while not a do while would be
  case 0:
    s = " 00000000"
    break;
ans.value += s;

If you put it into a function you could remove the breaks and just have return " 0..." + s.

A million ways to do the same thing with code, all about picking what you like best when it is your own project.

1 Like