Help me fix the code for binary to text translator

조회 수: 1 (최근 30일)
Hiren Pansuriya
Hiren Pansuriya 2020년 1월 9일
답변: David Hill 2020년 1월 9일
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