MATLAB Answers

Help me fix the code for binary to text translator

7 views (last 30 days)
Hiren Pansuriya
Hiren Pansuriya on 9 Jan 2020
Answered: David Hill on 9 Jan 2020
Hi experts,
I am developing one binary translator like this: https://www.gradecalculator.tech/binary-translator/
The confusion is only in javascript function. The code is given below that I used in my tool. I don't know what's wrong with the code but I get error in console like ($ is not a function). Please help me to solve this issue. Thanks in advance.
function BtoText()
{
var bin = $("#bin").val();
var i=$(".calc select")[0].selectedIndex;
if( i==0 )
bin = bin.match(/[0-1]{1,8}/g);
else
bin = bin.match(/[0-1]*/g).filter(function (el) {
return el!="";
});
if( !bin ) return;
len = bin.length;
if( len==0 ) return;
txt='';
for(i=0; i<len; i++)
{
b = bin[i];
code = parseInt(b,2);
t = String.fromCharCode(code);
txt += t;
}
$("#txt").val(txt);
}

  0 Comments

Sign in to comment.

Answers (1)

David Hill
David Hill on 9 Jan 2020
You need to decide what the format of your input binary will look like. Will you force the input to be multiples of 8-bits? If your input is a character string of binary numbers at multiple of 8-bits long, then:
function Output=b2text(Input)%Input'010010000110010101101100011011000110111101110111'
Output=char(bin2dec(reshape(Input,8,[])')');
end

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags