473,396 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

base64

Jay
I have bean trying to get my head around reading .GIF files from base64
strings,
Basically I need to specify a filename and convert it to base64 then I
can copy/past the string to wear I want it.
Cold somebody check this for me to see what I have done wrong:

If you run this program and enter a path for a .GIF file into the white
box and hit <Enter> it should display the string in the blue box.

To test this if you click the menu File Display it will create a top
level window displaying the image and giving the original string
underneath.

The bit I can not understand:

It will generate the base64 string but when trying to display it, it
reads the string up until the first ("/n") ("Cg==") <Return>
and then stops.

The base 64 string seams to be complete but when converted back it is
incomplete.

Thanks

Jay Dee

........................START..................... .

from Tkinter import *
from base64 import *

################################################## ##########################
################################################## ##########################
# Module: Base64 Encoder / Decoder.py
# Author: Jay Dee
# Date: 08/04/2006
# Version: Draft 0.1
# Coments: A Base64 Encoder / Decoder for converting files into Base64
strings
################################################## ##########################
################################################## ##########################

class App:
def __init__(self, root):
root.title("Base64 Encoder / Decoder")
################################################## ##########################
# Menu Bar
################################################## ##########################
self.menubar = Menu(root)
# create a pulldown menu, and add it to the menu bar
self.filemenu = Menu(self.menubar, tearoff=0)
self.filemenu.add_command(label="Display!",
command=self.Display)
self.filemenu.add_separator()
self.filemenu.add_command(label="Quit!", command=root.quit)
self.menubar.add_cascade(label="File", menu=self.filemenu)
# display the menu
root.config(menu=self.menubar)
################################################## ##########################
# Main
################################################## ##########################
# File Input
self.FileInputFrame = Frame(root)
self.FileInputFrame.pack(side=TOP, fill=X)

self.FileInputLine = Entry(self.FileInputFrame,
bg="white",
width=70)
self.FileInputLine.pack(side=LEFT, fill=X, expand=1)
# Display Area
self.DisplayFrame = Frame(root)
self.DisplayFrame.pack(side=TOP, fill=BOTH, expand=1)

self.DisplayText = Text(self.DisplayFrame,
bg="lightblue",
width=95,
height=40)
self.DisplayText.pack(side=LEFT, fill=BOTH, expand=1)

root.bind("<Return>",self.Encode)

def Encode(self,event):
'''
Take's the string from (self.FileInputLine),
converts it to base64 then desplays it in (self.DisplayText)
'''
self.DisplayText.delete(1.0,END)

info = self.FileInputLine.get()
if info == "":
self.DisplayText.insert(END, "...Empty String...")
else:
try:
file = open(info)
try:
while 1:
line = file.readline()
if not line:
break
Encode = b64encode(line)
self.DisplayText.insert(END, Encode)
except:
self.DisplayText.insert(END, "...Data Problem...")

except:
self.DisplayText.insert(END, "...No Sutch File...")

def Display(self):
'''
Take's the string from (self.DisplayText), Creats a topleval
frame and displays the Data
'''
info = self.DisplayText.get(1.0,END)
# Display as Image
try:
self.DisplayImage = Toplevel()

self.InfoDisplay = PhotoImage(data=info)
PhotoSize =
[self.InfoDisplay.width(),self.InfoDisplay.height()]

self.DisplayImageCanvas = Canvas(
self.DisplayImage,
width=150,
height=PhotoSize[1] + 15)
self.DisplayImageCanvas.pack(fill=BOTH, expand=1)
self.InfoDisplay2 = self.DisplayImageCanvas.create_image(
PhotoSize[0] / 2 + 10,
PhotoSize[1] / 2 + 10,
image=self.InfoDisplay)
self.InfoDisplay2 = self.DisplayImageCanvas
except:pass
# Display as Text
self.DisplayImageText = Text(
self.DisplayImage,
width=70,
height=30)
self.DisplayImageText.pack(fill=BOTH, expand=1)

Decode = b64decode(info)
self.DisplayImageText.insert(END, Decode)
root = Tk()
app = App(root)
root.mainloop()

################################################## ###########################

Apr 12 '06 #1
5 4097
On 13/04/2006 4:07 AM, Jay wrote:
I have bean trying to get my head around reading .GIF files from base64
strings,
FROM base64? Sounds a tad implausible.
Basically I need to specify a filename and convert it to base64 then I
can copy/past the string to wear I want it.
TO base64? That's better, provided the referent of the first "it" is the
file contents, not the filename.
Cold somebody check this for me to see what I have done wrong:
The bit I can not understand:

It will generate the base64 string
No it doesn't and no it can't; it's reading the *FILE* one "line" at a
time. This is in fact the root cause of your problem; a GIF file is a
binary file; there are no "lines"; any '\n' or '\r' characters are
binary data. Why is it stopping early? Possibly there is a ctrl-Z
character in the file and you are running on Windows. You need to open
the file with "rb" as the second arg, and read the whole file in as one
string. *After* encoding it, you can break up the base64 string into
bite-size chunks, append a newline (that's "\n", NOT "/n") to each
chunk, and send it over a 7-bit-wide channel.

You may wish to try a small console script that might help you
understand what's going on:

# Input: name of file as 1st arg
# Output: base64 encoding written to stdout in 64-byte chunks
import sys, base64
CHUNKSIZE = 64
fname = sys.argv[1]
fhandle = open(fname, "rb")
fcontents = fhandle.read()
b64 = base64.b64encode(fcontents)
for pos in xrange(0, len(b64), CHUNKSIZE):
print b64[pos:pos+CHUNKSIZE]

[snip]

def Encode(self,event):
'''
Take's the string from (self.FileInputLine),
converts it to base64 then desplays it in (self.DisplayText)
'''
The above documentation reflects neither what the method is doing now
nor what it should be doing. The latter is something like:

Takes (LTFA!) a filename from self.FileInputLine
Opens the file in binary mode
Encodes the file's contents as base64
Displays the encoded string in self.DisplayText
self.DisplayText.insert(END, "...No Sutch File...")


Is the GIF file meant to be a photo of the late Screaming Lord?

HTH,
John
Apr 12 '06 #2
John Machin wrote:
*After* encoding it, you can break up the base64 string into
bite-size chunks, append a newline (that's "\n", NOT "/n") to each
chunk /.../


base64.encodestring(data) does all that in one step, of course.

</F>

Apr 12 '06 #3
On 13/04/2006 7:22 AM, Fredrik Lundh wrote:
John Machin wrote:
*After* encoding it, you can break up the base64 string into
bite-size chunks, append a newline (that's "\n", NOT "/n") to each
chunk /.../


base64.encodestring(data) does all that in one step, of course.


and it's tagged as part of the "legacy interface", and gives no control
over the chuck size, of course :-)
Apr 12 '06 #4
John Machin wrote:
base64.encodestring(data) does all that in one step, of course.


and it's tagged as part of the "legacy interface", and gives no control
over the chuck size, of course :-)


if you read the documentation, it's clear that legacy means "base64 only",
not "deprecated". it uses the chunk size specified by the MIME standard,
which is the traditional Base64 reference.

it's not like Base16 and Base32 are new things that will soon overtake the
old and little used Base64-according-to-MIME encoding...

</F>

Apr 13 '06 #5
Jay
I don't know whether it is right yet but it dues what I wanted it to
do now so thank you all,

Oh and sorry for my bad grammar.

One last thing though is that I would like to be able to split the
string up into lines of 89 carictors, I have lookd through the split
methods and all I can find is splitting up words and splitting at a
pacific caricature, I can not see how you split after a number of
caricatures
........................START..................... .

from Tkinter import *
from base64 import *

################################################## ###################
################################################## ###################
# Module: Base64 Encoder / Decoder.py
# Author: Jay Dee
# Date: 08/04/2006
# Version: Draft 0.1
# Coments: A Base64 Encoder / Decoder for converting files into Base64
strings
################################################## ###################
################################################## ###################

class App:
def __init__(self, root):
root.title("Base64 Encoder / Decoder")
################################################## ###################
# Menu Bar
################################################## ###################
self.menubar = Menu(root)
# create a pulldown menu, and add it to the menu bar
self.filemenu = Menu(self.menubar, tearoff=0)
self.filemenu.add_command(label="Display!",
command=self.Display)
self.filemenu.add_separator()
self.filemenu.add_command(label="Quit!", command=root.quit)
self.menubar.add_cascade(label="File", menu=self.filemenu)
# display the menu
root.config(menu=self.menubar)
################################################## ###################
# Display B64 Text
################################################## ###################
# File Input
self.FileInputFrame = Frame(root)
self.FileInputFrame.pack(side=TOP, fill=X)

self.FileInputLine = Entry(self.FileInputFrame,
bg="white",
width=70)
self.FileInputLine.pack(side=LEFT, fill=X, expand=1)
# Display Area
self.DisplayFrame = Frame(root)
self.DisplayFrame.pack(side=TOP, fill=BOTH, expand=1)

self.DisplayText = Text(self.DisplayFrame,
bg="lightblue",
width=95,
height=40)
self.DisplayText.pack(side=LEFT, fill=BOTH, expand=1)

root.bind("<Return>",self.Encode)

def Encode(self,event):
'''
Take's the file name from (self.FileInputLine),
opens file,
converts it to base64 then desplays it in (self.DisplayText)
'''
self.DisplayText.delete(1.0,END)

info = self.FileInputLine.get()
if info == "":
self.DisplayText.insert(END, "...Please enter file...")
else:
try:
file = open(info,"rb")
try:
Data = file.read()
Encode = b64encode(Data)
Encode = Encode.split()
self.DisplayText.insert(END, Encode)
except:
self.DisplayText.insert(END, "...Data Erra...")

except:
self.DisplayText.insert(END, "...No Sutch File...")

def Display(self):
'''
Take's the string from (self.DisplayText), Creats a topleval
frame and displays the Data as an image,if that fales it
displays it as text.
'''
info = self.DisplayText.get(1.0,END)
# Display as Image
try:
self.DisplayImage = Toplevel()

self.InfoDisplay = PhotoImage(data=info)
PhotoSize =
[self.InfoDisplay.width(),self.InfoDisplay.height()]

self.DisplayImageCanvas = Canvas(
self.DisplayImage,
width=150,
height=PhotoSize[1] + 15)
self.DisplayImageCanvas.pack(fill=BOTH, expand=1)
self.InfoDisplay2 = self.DisplayImageCanvas.create_image(
PhotoSize[0] / 2 + 10,
PhotoSize[1] / 2 + 10,
image=self.InfoDisplay)
self.InfoDisplay2 = self.DisplayImageCanvas

# Display as Text
except:
self.DisplayBase64Text = Text(
self.DisplayImage,
width=70,
height=30)
self.DisplayBase64Text.pack(fill=BOTH, expand=1)

Decode = b64decode(info)
self.DisplayImageText.insert(END, Decode)
root = Tk()
app = App(root)
root.mainloop()

Apr 13 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Karl Pech | last post by:
Hi all, I'm trying to write a program which can read in files in the following format: sos_encoded.txt: --- begin-base64 644 sos.txt UGxlYXNlLCBoZWxwIG1lIQ== ---
27
by: gRizwan | last post by:
Hello all, We have a problem on a webpage. That page is sent some email data in base64 format. what we need to do is, decode the base64 data back to original shape and extract attached image...
4
by: John | last post by:
Hi all, I've been going through google and yahoo looking for a certain base64 decoder in C without success. What I'm after is something that you can pass a base64 encoded string into and get back...
0
by: Phil C. | last post by:
(Cross post from framework.aspnet.security) Hi. I testing some asp.net code that generates a 256 bit Aes Symmetric Key and a 256 bit entropy value. I encrypt the Aes key(without storing it as...
7
by: Neo Geshel | last post by:
Greetings. I have managed to stitch together an awesome method of posting text along with an image to a database, in a way that allows an unlimited number of previews to ensure that text and...
4
by: Russell Warren | last post by:
I've got a case where I want to convert binary blocks of data (various ctypes objects) to base64 strings. The conversion calls in the base64 module expect strings as input, so right now I'm...
1
by: Roland Rickborn | last post by:
Hallo zusammen, in meine Anwendung ist ein Bild eingebettet und oben in der Leiste soll ein Icon erscheinen. Ausserdem will ich nur _eine_ Datei ausgeben, also ohne zusärtliche Bild-Dateien...
13
by: aruna.eies.eng | last post by:
i am currently trying to convert data into binary data.for that i need to know how to achieve it in c language and what are the libraries that we can use. so if any one can send me a sample code or...
10
by: pycraze | last post by:
Hi , I am currently trying to implement base64 encoding and decoding scheme in C . Python has a module , base64 , that will do the encoding and decoding with ease . I am aware of OpenSSL having...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.