Binary to String conversion

This is the place for queries that don't fit in any of the other categories.

Binary to String conversion

Postby Beginner123 » Sun Apr 20, 2014 1:23 pm

I'm currently trying to work on a program which takes a binary number and converts it to a String of text.

if someone could help me out ASAP that would be great.

Thanks
Last edited by Mekire on Sun Apr 20, 2014 2:32 pm, edited 1 time in total.
Reason: First post lock.
Beginner123
 
Posts: 5
Joined: Sun Apr 20, 2014 1:18 pm

Re: Binary to String conversion

Postby metulburr » Sun Apr 20, 2014 3:55 pm

Code: Select all
def to_binary(s):
    return ''.join(['%08d'%int(bin(ord(i))[2:]) for i in str(s)])
   
def to_string(b):
    return ''.join(chr(int(b[i:i+8], 2)) for i in range(0, len(b), 8))
   
print(to_binary('string'))
print(to_string('01110100011001010111100001110100'))
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1455
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Binary to String conversion

Postby Beginner123 » Sun Apr 20, 2014 4:09 pm

where about's would i add my own Binary to that code?

thanks a lot btw
Beginner123
 
Posts: 5
Joined: Sun Apr 20, 2014 1:18 pm

Re: Binary to String conversion

Postby Mekire » Sun Apr 20, 2014 4:13 pm

You really need to do some work here.
What is your desired input?
What is your desired output?
What have you tried?
How doesn't it work (error/undesired result)?
Etc.

Help us help you.
-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Binary to String conversion

Postby Beginner123 » Sun Apr 20, 2014 4:22 pm

what i have atm is an input which takes a simple String of Text, converts it to binary then hides it in a picture using Pillow.
then i have a second program which takes the picture and finds the hidden binary but i don't know how to convert the binary back to text. I'm just experimenting with putting the binary from the first part direct into this because the part of hiding and finding it on a picture is on another computer which i don't have access to atm and not on my personal laptop.
thanks
Beginner123
 
Posts: 5
Joined: Sun Apr 20, 2014 1:18 pm

Re: Binary to String conversion

Postby 7stud » Sun Apr 20, 2014 5:04 pm

In your opinion, what is a binary number? Because there is no such thing. You can have a string written in binary format that represents a number. And there are numbers, which computers internally store in binary format--but since all numbers (and even characters) are stored in binary on a computer, how does calling something a binary number distinguish it from all other numbers?
7stud
 
Posts: 99
Joined: Wed Apr 02, 2014 2:36 am

Re: Binary to String conversion

Postby Beginner123 » Sun Apr 20, 2014 5:11 pm

well ill show you from my program what it gives when i simply put in "hello":
Code: Select all
0b11100110
0b1011000
96
6
0b11100110
0b1010101
96
6
0b11100110
0b1011100
96
6
0b11100110
0b1011100
96
6
0b11100110
0b1011111
96
6
Beginner123
 
Posts: 5
Joined: Sun Apr 20, 2014 1:18 pm

Re: Binary to String conversion

Postby 7stud » Sun Apr 20, 2014 5:19 pm

Hmm...complete gibberish. You are going to have to give us the details of your encoder ring.
7stud
 
Posts: 99
Joined: Wed Apr 02, 2014 2:36 am

Re: Binary to String conversion

Postby Beginner123 » Sun Apr 20, 2014 5:23 pm

the program for converting it from a string is bellow:

Code: Select all
import string

a = input("please enter the text you want to hide: ")

for ch in a:
    b = (ch)
    c = ord(ch)
    d = bin (c)
    e = 231
    f = 86   
    g = (((c&240) >> 4)|(e&240))
    h = (((c&15)|(f&240)))

    i=bin(g)
    j=bin(h)

    print (i)
    print(j)
   
    print(c&240)
    print((c&240)>>4)
Last edited by Mekire on Sun Apr 20, 2014 10:09 pm, edited 1 time in total.
Reason: Code tags.
Beginner123
 
Posts: 5
Joined: Sun Apr 20, 2014 1:18 pm

Re: Binary to String conversion

Postby 7stud » Sun Apr 20, 2014 5:36 pm

Oh yes, your program just 'converts to binary'. What do you think all those &, |, and >> things do? You have to reverse all those operations. Start studying the topic 'bitwise operations'.
7stud
 
Posts: 99
Joined: Wed Apr 02, 2014 2:36 am

Re: Binary to String conversion

Postby Mekire » Sun Apr 20, 2014 10:32 pm

What does the method that Metulburr showed you not do, that you are trying to do?
Code: Select all
def to_binary(s):
    return ''.join(['%08d'%int(bin(ord(i))[2:]) for i in str(s)])

def to_string(b):
    return ''.join(chr(int(b[i:i+8], 2)) for i in range(0, len(b), 8))

change_to_binary = to_binary('What is it you want?')
change_back_to_string = to_string(change_to_binary)

print(change_to_binary)
print(change_back_to_string)
Result:
Code: Select all
>>>
0101011101101000011000010111010000100000011010010111001100100000011010010111010000100000011110010110111101110101001000000111011101100001011011100111010000111111
What is it you want?
>>>

I have to agree with 7stud here. You look like you are just doing a bunch of random bitwise operations on your numbers with no real goal.
-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Binary to String conversion

Postby mckryall » Sun Apr 20, 2014 11:05 pm

Were you provided with this program, or did you make it yourself? Either way, I want to know where you/they learned about this. Also, are you sure that you're outputting to binary and not just encoding something into some made-up codec with a script?
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests