Help me fix the code for binary to text translator

1 次查看(过去 30 天)
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);
}

回答(1 个)

David Hill
David Hill 2020-1-9
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

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by