Results 1 to 6 of 6

Thread: Javascript code for a binary translator?

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    5

    Javascript code for a binary translator?

    Hello,

    I ma working on a freelancing project with Angular and .net. For this project, my client wants me to create a binary translator which can convert binary to text and text to binary. Initially, i used .net server for this operation and everything worked fine. But now, I think using server such small conversion is not good and I can do this conversion in the front-end and after the conversion, I will send a value to the back-end.

    i have following Javascript code which works well with small digits but does not work well with the bigger digits and returns wrong answer for such numbers.

    Code:
    function convertBinary() {
      var output = document.getElementById("outputBinary");
      var input = document.getElementById("inputBinary").value;
      output.value = "";
      for (i = 0; i < input.length; i++) {
        var e = input[i].charCodeAt(0);
        var s = "";
        do {
          var a = e % 2;
          e = (e - a) / 2;
          s = a + s;
        } while (e != 0);
        while (s.length < 8) {
          s = "0" + s;
        }
        output.value += s;
      }
    }
    Last edited by dday9; Feb 8th, 2021 at 01:36 PM.

  2. #2
    New Member
    Join Date
    Sep 2020
    Location
    Daly City
    Posts
    4

    Re: Javascript code for a binary translator?

    This is interesting. This code seems fine for bigger numbers as well. Exactly what issue you are facing?

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    5

    Re: Javascript code for a binary translator?

    See, ultimately I have to do what client tells.

    Issue is when someone inserts a A in the box, it returns a correct answer until the inserted value is 7-bit or 8-bit. If someone adds a special character in the box then the tool does not return correct answer.

    For example, the real answer for a special character ";" is 00111011 while this tool returns 0111011 because this code is designed for just 7 and 8-bit numbers.

    I sorted the issue of bigger numbers with bignumber.js.

  4. #4
    New Member
    Join Date
    Sep 2020
    Location
    Daly City
    Posts
    4

    Re: Javascript code for a binary translator?

    You could simply use if else condition for the conversion. For example, for an answer with 7-bit number add a zero, for an answer with -bit number add two zeros.

    Like this:

    if(s.length === 7) {
    s = " 0" + s;
    this.ans += s;
    }
    if(s.length === 6) {
    s = " 00" + s;
    this.ans += s;
    }
    if(s.length === 5) {
    s = " 000" + s;
    this.ans += s;
    }
    if(s.length === 4) {
    s = " 0000" + s;
    this.ans += s;
    }
    if(s.length === 3) {
    s = " 00000" + s;
    this.ans += s;
    }
    i found it here: https://binarytotext.net/. This is not the best solution but they have done this and i assume it works fine.

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2021
    Posts
    5

    Re: Javascript code for a binary translator?

    LOL, this piece actually worked. But just out of curiosity, is there any other cleaner way we can do this?

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Javascript code for a binary translator?

    Tack on "0000000" to the front, then take the right 8 ...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width