Problem of the Day
A new programming or logic puzzle every Mon-Fri

Binary 2 ASCII Converter

Building off of yesterday's problem let's create a binary to ASCII converter. The string "0101100101101111011101010010000001110011011011110110110001110110011001010110010000100000011010010111010000100001" should map to "You solved it!". If it helps, you only need to account for characters 32-126 from the ASCII table.

As a bonus create a way to go from ASCII to binary.

Permalink: http://problemotd.com/problem/binary-2-ascii-converter/

Comments:

  • Anonymous - 10 years, 5 months ago

    #!/usr/bin/env python2
    bits = '0101100101101111011101010010000001110011011011110110110001110110011001010110010000100000011010010111010000100001'
    print ''.join(chr(int(''.join(map(str,map(int, ''.join([c for c in bits ]))[i:i+8])),2)) for i in range(0,len(map(int, ''.join([c for c in bits ]))),8))
    

    reply permalink

  • pyrohaze - 10 years, 5 months ago

    Python solution

    binary=raw_input("input?\n")
    binary_sep = [binary[8*i:8*(i+1)] for i in xrange(len(binary)//8)]
    ints = [int(x, 2) for x in binary_sep]
    chars = [chr(x) for x in ints]
    print ''.join(chars)
    

    reply permalink

  • Anonymous - 10 years, 5 months ago

    Clojure:

    (defn binToText [bin]
      (if (< 8 (count bin))
          (str (char (Integer/parseInt (subs bin 0 8) 2)) 
               (binToText (subs bin 8)))))
    

    reply permalink

  • Anonymous - 10 years, 5 months ago

    #python 2.7
    a = raw_input("Enter a string> ")
    b = ""
    for letters in a:
        b = b + str(bin(ord(letters))[2:9])
    
    print b
    

    reply permalink

  • Anonymous - 10 years, 5 months ago

    That's ASCII to Binary. People already solved the other way.

    reply permalink

  • bumbleguppy - 10 years, 5 months ago

    javascript

    function binary2ascii(msg){
        var returnValue = '';
        var bytePad = ''
        var i;
        var len = msg.length;
        if(msg.replace(/[01]*/g,'').length){
            for(i = 0; i < len; i++){
                bytePad = msg.charCodeAt(i).toString(2);
                if(bytePad.length < 8){
                    bytePad = ('00000000' + bytePad).substr(bytePad.length); 
                }
                returnValue += bytePad;
            }
        }else{
            for(i = 0;i < len; i+=8){
                returnValue += String.fromCharCode(parseInt(msg.substring(i, (i + 8)), 2));
            }
        }
        return returnValue;
    }
    
    console.log(binary2ascii('0101100101101111011101010010000001110011011011110110110001110110011001010110010000100000011010010111010000100001'));
    
    console.log(binary2ascii('You solved it!'));
    

    reply permalink

  • Anonymous - 10 years, 5 months ago

    #python 2.7
    
    def bin_to_ascii(binarynumber):
        i = 0
        output = ""
        while i < len(binarynumber):
            output = output + chr(int("0b" + binarynumber[i: i+8], 2))
            i = i + 8
        return output
    
    def ascii_to_bin(script):
        ascii = ""
        for letter in script:
            ascii = ascii + ("{0:08d}".format(int(bin(ord(letter))[2:])))
    
        return ascii
    

    reply permalink

Content curated by @MaxBurstein