-
Feb 8th, 2021, 08:34 AM
#1
Thread Starter
New Member
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.
-
Feb 8th, 2021, 11:36 PM
#2
New Member
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?
-
Feb 9th, 2021, 08:44 AM
#3
Thread Starter
New Member
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.
-
Feb 9th, 2021, 01:28 PM
#4
New Member
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.
-
Feb 9th, 2021, 11:11 PM
#5
Thread Starter
New Member
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?
-
Feb 10th, 2021, 05:40 PM
#6
Re: Javascript code for a binary translator?
Tack on "0000000" to the front, then take the right 8 ...
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|