-1

Hi my program print on this way:

Mesagge to binary:asd

01100001
01110011
01100100

but i need to print the message in form horizontal like this (011000010111001101100100)

Ive been trying everything i don know how to do it.

def string_a_binario(s):

    for i in range (0, len(s)):

        if s[i] == " " : 
            print("00100001")
        elif s[i] == "!" :
            print("00100001")

        elif s[i] == "#" :
            print("00100011")
        elif s[i] == "$" :
            print("00100100")
        elif s[i] == "%" :
            print("00100101")
        elif s[i] == "&" :
            print("00100110")
        elif s[i] == "'" :
            print("00100111")
        elif s[i] == "(" :
            print("00101000")
        elif s[i] == ")" :
            print("00101001")
        elif s[i] == "*" :
            print("00101010")
        elif s[i] == "+" :
            print("00101011")
        elif s[i] == "," :
            print("00101100")
        elif s[i] == "-" :
            print("00101101")
        elif s[i] == "." :
            print("00101110")
        elif s[i] == "/" :
            print("00101111")
        elif s[i] == "0" :
            print("00110000")
        elif s[i] == "1" :
            print("00110001")
        elif s[i] == "2" :
            print("00110010")
        elif s[i] == "3" :
            print("00110011")
        elif s[i] == "4" :
            print("00110100")
        elif s[i] == "5" :
            print("00110101")
        elif s[i] == "6" :
            print("00110110")
        elif s[i] == "7" :
            print("00110111")
        elif s[i] == "8" :
            print("00111000")
        elif s[i] == "9" :
            print("00111001")
        elif s[i] == ":" :
            print("00111010")
        elif s[i] == ";" :
            print("00111011")
        elif s[i] == "<" :
            print("00111100")
        elif s[i] == "=" :
            print("00111101")
        elif s[i] == ">" :
            print("00111110")
        elif s[i] == "?" :
            print("00111111")
        elif s[i] == "@" :
            print("01000000")
        elif s[i] == "A" :

            print("01000001")
        elif s[i] == "B" :
            print("01000010")

        elif s[i] == "C" :
            print("01000011")

        elif s[i] == "D" :
            print("01000100")
        elif s[i] == "E" :
            print("01000101")
        elif s[i] == "F" :
            print("01000110")
        elif s[i] == "G" :
            print("01000111")
        elif s[i] == "H" :
            print("01001000")
        elif s[i] == "I" :
            print("01001001")
        elif s[i] == "J" :
            print("01001010")
        elif s[i] == "K" :
            print("01001011")
        elif s[i] == "L" :
            print("01001100")
        elif s[i] == "M" :
            print("01001101")
        elif s[i] == "Ñ" :
            print("01001110")
        elif s[i] == "O" :
            print("01001111")
        elif s[i] == "P" :
            print("01010000")
        elif s[i] == "Q" :
            print("01010001")
        elif s[i] == "R" :
            print("01010010")
        elif s[i] == "O" :
            print("01001111")
        elif s[i] == "P" :
            print("01010000")
        elif s[i] == "Q" :
            print("01010001")
        elif s[i] == "R" :
            print("01010010")
        elif s[i] == "S" :
            print("01010011")

        elif s[i] == "T" :
            print("01010100")
        elif s[i] == "U" :
            print("01010101")
        elif s[i] == "V" :
            print("01010110")
        elif s[i] == "W" :
            print("01010111")
        elif s[i] == "X" :
            print("01011000")
        elif s[i] == "Y" :
            print("01011001")
        elif s[i] == "Z" :
            print("01011010")

        elif s[i] == "[" :
            print("01011011")
        elif s[i] == " \ "  :
            print("01011100")
        elif s[i] == "]" :
            print("01011101")
        elif s[i] == "^" :
            print("01011110")
        elif s[i] == "_" :
            print("01011111")
        elif s[i] == "`" :
            print("01100000")


        elif s[i] == "a" :
            print("01100001")
        elif s[i] == "b" :
            print("01100010")
        elif s[i] == "c" :
            print("01100011")
        elif s[i] == "d" :
            print("01100100")
        elif s[i] == "e" :
            print("01100101")
        elif s[i] == "f" :
            print("01100110")
        elif s[i] == "g" :
            print("01100111")
        elif s[i] == "h" :
            print("01101000")
        elif s[i] == "i" :
            print("01101001")
        elif s[i] == "j" :
            print("01101010")
        elif s[i] == "k" :
            print("01101011")
        elif s[i] == "l" :
            print("01101100")
        elif s[i] == "m" :
            print("01101101")
        elif s[i] == "n" :
            print("01101110")
        elif s[i] == "o" :
            print("01101111")
        elif s[i] == "p" :
            print("01110000")
        elif s[i] == "q" :
            print("01110001")
        elif s[i] == "r" :
            print("01110010")
        elif s[i] == "s" :
            print("01110011")
        elif s[i] == "t" :
            print("01110100")
        elif s[i] == "u" :
            print("01110101")
        elif s[i] == "v" :
            print("01110110")
        elif s[i] == "w" :
            print("01110111")
        elif s[i] == "x" :
            print("01111000")
        elif s[i] == "y" :
            print("01111001")
        elif s[i] == "z" :
            print("01111010")
        elif s[i] == "{" :
            print("01111011")
        elif s[i] == "|" :
            print("01111100")
        elif s[i] == "}" :
            print("01111101")
        elif s[i] == "~" :
            print("01111110")

s = list(input("Message to binary:"))
string_a_binario(s)
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
Felipe
  • 41

3 Answers3

3

As for the multiple lines issue, if you put a comma (,) at the end of a print statement, it will not print a newline character:

Example:

print 'Put these on',
print 'just one line',
print 'please'

Prints:

Put these on just one line please

Note: This still prints spaces after each item. If that is unacceptable, use sys.stdout.write() like Mai showed.


There is a much better way to accomplish this than hard-coding every case! (I can't imagine how long it took you to write that!)

Hints:

Other python hints:

  • There's no need to convert the result of input to a list - strings in python are iterable.

  • This isn't C - you don't need to iterate with an array index. Instead you can iterate over each item in the sequence. Here, those items are each character of the string!

for i in range (0, len(s)):
    if s[i] == " " :
       ...

Can be replaced with

for x in s:
    is x == " ":
        ...
Community
  • 1
  • 1
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
1

Say I have two string s1 and s2:

import sys
sys.stdout.write(s1)
sys.stdout.write(s2)
sys.stdout.write('\n')
sys.stdout.flush()

Then s1 and s2 will be printed in the same line.

Hope it helps:)

Patrick the Cat
  • 2,138
  • 1
  • 16
  • 33
0

I just want to address how you wrote your function, it may introduce some error. For example, look at the first two if statements in your loop, it looks like the space and ! characters both output 00100001, was that desired? Here is something that is sort of similar to your function, and potentially takes out some human error.

def string_a_binario(s):
    for i in range(len(s)):
        sys.stdout.write("{0:08b}".format(ord(s[i])))

This code also writes horizontally as you desired. But, it does not do the same thing as your function for all input. For example your code prints nothing if none of the conditions are ever met, while this function could print something where yours could not.

In python 3 (and maybe previous versions, I am not for sure) you can tell print not to include the newline, here is another version of the function:

def string_a_binario(s):
    for i in range(len(s)):
        print("{0:08b}".format(ord(s[i])), end='')

Here is yet another version in python 3 which puts a bracket around each binary representation:

def string_a_binario(s):
    for i in range(len(s)):
        print("[{0:08b}".format(ord(s[i])), end=']')
Leonardo
  • 1,452
  • 3
  • 15
  • 26