Advanced Search

Results 1 to 10 of 10

Thread: ASCII -> Binary Conversion

  1. #1
    Join Date
    Mar 2011
    Location
    N 11░ 19' 0.0012 E 142░ 15' 0
    Posts
    1,508
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default ASCII -> Binary Conversion

    Hey everybody!
    Would anyone mind explaining how to convert ASCII bytes into their Binary form? (without using conversion tables)
    The language doesn't actually matter, I'm just looking for the method of conversion.
    I've tried googling it but haven't found anything overly helpful.
    Last edited by keyboard; 12-11-2013 at 02:35 AM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,705
    Thanks
    43
    Thanked 3,133 Times in 3,097 Posts
    Blog Entries
    12

    Default

    Do you mean just converting the hex and/or decimal designation of an ASCII character to a binary number?

    Probably not, but if there's any correlation, I would think that would be the only way to do it without a table because on a computer, in the end everything is just a 1 or a 0.

    For single characters, one at a time, in javascript that's:

    Code:
    function ascii2bin(c){
    	return c.charCodeAt(0).toString(2);
    }
    Last edited by jscheuer1; 12-10-2013 at 02:16 PM. Reason: add code
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,629
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    similarly, PHP has the ord function.
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  4. #4
    Join Date
    Mar 2011
    Location
    N 11░ 19' 0.0012 E 142░ 15' 0
    Posts
    1,508
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    Sorry for the confusion: I do mean converting the ASCII value (E.g. G=71) to its binary counterpart.
    I found this website that shows how to do it manually. It should be translatable I guess.

    --->
    Here's how I did it.
    HTML Code:
    <!DOCTYPE html>
    <html>
        <head>
            <script type="text/javascript" src="jquery.js"></script>
            <script type="text/javascript">
                $(function() {
                    $("#submit1").click(function() {
                        data = $("#input1").val();
                        num = 0;
                        iBuffer = "";
                        while(data > 0) 
                        {
                            num = data % 2;
                            data = Math.floor(data / 2);
                            iBuffer += num;
                        }
                        iBuffer = iBuffer.split("").reverse().join("");
                        $("body").append('<br />' + iBuffer);
                        return false;
                    });
                });
            </script>
        </head>
        <body>
            <textarea cols="10" rows="10" id="input1">71</textarea>
            <input type="submit" value="Go" id="submit1"/>
        </body>
    </html>

  5. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,629
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    PHP Code:
    <?php

    $chr 
    'a';             // a

    $ord ord$chr );     // 97

    $bin decbin$ord );  // 1100001
    We Only Torture the Folks We Don't Like (You're Probably Gonna Be Okay)
    It's a Party in the CIA

  6. #6
    Join Date
    Mar 2011
    Location
    N 11░ 19' 0.0012 E 142░ 15' 0
    Posts
    1,508
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    Yes, I did read that link you posted before traq. However I'd prefer to do the conversion without using an inbuilt function so it can be transferred across languages.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    Fair enough. But maybe this?
    http://phpjs.org/
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Mar 2011
    Location
    N 11░ 19' 0.0012 E 142░ 15' 0
    Posts
    1,508
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    I don't mean just compatible between JS/PHP. I mean other languages as well. Anyways my question has been answered

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    Well, one nice thing about phpjs is that it gives you the Javascript code for the PHP functions-- you can copy it to whatever you want
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,705
    Thanks
    43
    Thanked 3,133 Times in 3,097 Posts
    Blog Entries
    12

    Default

    I see. Most if not all languages have number conversion routines. In your solution you use one of them (% - modulus), which converts a number to it's remainder when divided by another. It's really a matter of how much of the math you want to 'do yourself'.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Java ASCII file output
    By jmace in forum Java
    Replies: 0
    Last Post: 02-01-2012, 11:08 PM
  2. Convert non-ASCII to HTML UNICODE entities
    By jscheuer1 in forum PHP
    Replies: 12
    Last Post: 03-29-2010, 11:17 PM
  3. ASCII art background help
    By maxkriz in forum CSS
    Replies: 2
    Last Post: 04-15-2009, 04:11 PM
  4. Testing for Bit Value of Binary Numbers
    By Strangeplant in forum PHP
    Replies: 9
    Last Post: 08-29-2006, 02:38 PM
  5. Binary Converter
    By mburt in forum JavaScript
    Replies: 5
    Last Post: 08-18-2006, 09:38 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •