1

I want to translate binary to text and back in a form, I could not find any examples on google or something.

I wanted to make something like this http://www.roubaixinteractive.com/PlayGround/Binary_Conversion/Binary_To_Text.asp

I found this code while searching but I cant get it working :#

    <script type="text/javascript">
    //<![CDATA[
    function txt2bin()
    {
        url = 'http://services.webestools.com/txt2bin/txt2bin.js?str='+encodeURIComponent($('ftxt').value);
        var js_effets=document.createElement("script");
        js_effets.setAttribute("type", "text/javascript");
        js_effets.setAttribute("src", url);
        document.getElementsByTagName("script")[0].parentNode.insertBefore(js_effets,document.getElementsByTagName("script")[0]);
        $('frm').style.display='block';
    }
    function bin2txt()
    {
        url = 'http://services.webestools.com/txt2bin/bin2txt.js?str='+encodeURIComponent($('fbin').value);
        var js_effets=document.createElement("script");
        js_effets.setAttribute("type", "text/javascript");
        js_effets.setAttribute("src", url);
        document.getElementsByTagName("script")[0].parentNode.insertBefore(js_effets,document.getElementsByTagName("script")[0]);
        $('frm').style.display='block';
    }
    //]]>
    </script>
0

1 Answer 1

1

Are you sure you want it in PHP?

PHP means you need a webserver that interprets your code. Your pasted snippet is in javascript, which is run by the browser and hence you don't need a server to just test it.

Anyway, doing it in javascript would actually be more efficient, if you want to create a tool like the one you linked.

2 functions in javascript to do the conversions:

function txt2bin(txt) {
    var pad = '00000000',
        bin = '',
        c = '';
    for(var i = 0, l = txt.length; i < l; i++) {
        c = txt.charCodeAt(i).toString(2);
        bin += (pad + c).substr(c.length);
    }
    return bin;
}

function bin2txt(bin) {
    var split = bin.match(/.{8}/g),
        txt = '';
    for(var i = 0, l = split.length; i < l; i++) {
        txt += String.fromCharCode(parseInt(split[i], 2));
    }
    return txt;
}

test them here: http://jsfiddle.net/S3nB7/

txt2bin takes an ASCII encoded string and returns a binary representation of it.

bin2txt takes a string of 0 and 1 that represent the binary code of an ASCII encoded string and returns the ASCII string.

In PHP this would translate to:

<?php

function txt2bin($txt) {
    $bin = '';
    for($i = 0, $l = strlen($txt); $i < $l; $i++) {
        $bin .= sprintf('%08b', ord(substr($txt, $i, 1)));
    }
    return $bin;
}

function bin2txt($bin) {
    $split = str_split($bin, 8);
    $txt = '';
    for($i = 0, $l = count($split); $i < $l; $i++) {
        $txt .= chr(bindec($split[$i]));
    }
    return $txt;
}

test here: http://codepad.org/Y3sFJrR7

Sign up to request clarification or add additional context in comments.

2 Comments

Well I wanted to do it in PHP because I know a bit of PHP and I know nothing in Javascript :p
I tried to get ur code working but I couldnt get it working :# jsfiddle.net/bhEyf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.