473,511 Members | 15,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

uuDecode problem

py
Hi,
I am encoding a string such as...

Expand|Select|Wrap|Line Numbers
  1. data = someFile.readlines()
  2. encoded = []
  3. for line in data:
  4. encoded.append(binascii.b2a_uu(stringToEncode))
  5. return encoded
  6.  
....I then try to decode this by...

Expand|Select|Wrap|Line Numbers
  1. def decode(data):
  2. result = []
  3. for val in data:
  4. result.append(binascii.a2b_uu(val))
  5. return result
  6.  
this seems to work sometimes....for example a list which has a short
string in it like ["this is a test"]

however if the list of data going into the decode function contains a
bunch of elements I get the following error...

result.append(binascii.a2b_uu(val))
binascii.Error: Trailing garbage

...any idea why this is happening? Anyone successfully use the uu to
encode/decode strings of varying length (even larger strings, more than
a few hundred characters)?

Dec 7 '05 #1
9 7272
py <co*******@gmail.com> wrote:
...
encoded.append(binascii.b2a_uu(stringToEncode))
binascii.b2a_uu only works for up to 45 bytes at once; but if you were
feeding it more than 45 bytes, this should raise a binascii.Error
itself.
..any idea why this is happening? Anyone successfully use the uu to
encode/decode strings of varying length (even larger strings, more than
a few hundred characters)?


Definitely not, given the above limit. But I still don't quite
understand the exact mechanics of the error you're getting.
Alex
Dec 7 '05 #2
py
Alex Martelli wrote:
binascii.b2a_uu only works for up to 45 bytes at once; but if you were
feeding it more than 45 bytes, this should raise a binascii.Error
itself.
Definitely not, given the above limit. But I still don't quite
understand the exact mechanics of the error you're getting.
Alex


here is an example.

def doSomething():
data = aFile.readlines()
result = []
for x in data:
result.append(encode(x))
return result

def printResult(encodedData):
"""encodedData is a list of strings which are uu encoded"""
print decode(encodedData)

encode(data):
"""data is a string"""
if len(data) > 45:
tmp = []
for c in data:
tmp.append(binascii.b2a_uu(c))
return ''.join(tmp)
else:
return binascii.b2a_uu(data)
decode(data):
"""data is a list of strings"""
result = []
for val in data
if len(val) > 45:
response = []
for x in val:
response.append(binascii.a2b_uu(x))
result.append(response)
else:
result.append(binascii.a2b_uu(val))
return ''.join(result)

....i would use those functions like

data = doSomething()
printResult(data)

Now i get this...
" response.append(binascii.a2b_uu(x))
java.lang.StringIndexOutOfBoundsException:
java.lang.StringIndexOutOfBoundsExcep
tion: String index out of range: 1"

So the error is in the decode method .....this is in Jython...perhaps
Jython doesn't handle binascii.a2b_uu ? or perhaps since the actual
data is being encoded in python, then read in and decoded in my jython
script..that could be the problem?

thanks.

Dec 7 '05 #3
Note that you can use the 'uu' encoding, which will handle
arbitrary-length input and give multi-line uuencoded output, including
the 'begin' and 'end' lines:
print ("\377" * 120).encode("uu")

Otherwise, you should use something like
encoded = [binascii.b2a_uu(chunk)
for chunk in iter(lambda: someFile.read(45), "")]
to send at most 45 bytes to each call to b2a_uu.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFDlw/oJd01MZaTXX0RAqIOAJ46t30KNHMT7tAHULcPQORmqKQ9PACgl BTh
GBjbFibJBu+GDx6cbtC53Us=
=GWjT
-----END PGP SIGNATURE-----

Dec 7 '05 #4
py <co*******@gmail.com> wrote:
...
"""data is a string"""
if len(data) > 45:
tmp = []
for c in data:
tmp.append(binascii.b2a_uu(c))


You can't decode b2a-encoded data character by character, blindly, as
you're trying to to here. Each character in the source string can be
encoded into multiple characters in the target string, and the slicing,
if slicing is needed, must be appropriate. I suggest a redesign...!
Alex
Dec 8 '05 #5
py
Alex Martelli wrote:
I suggest a redesign...!
What would you suggest? I have to encode/decode in chunks b/c of the
45 byte limitation.

Thanks.

Dec 9 '05 #6
"py" wrote:
What would you suggest? I have to encode/decode in chunks b/c of the
45 byte limitation.


so use 45-byte chunks, instead of one-byte chunks.

but why are you using UU encoding in a nonstandard way ? why not just
use the "uu" module to do the chunking for you? the third example on this
page might be helpful:

http://effbot.org/librarybook/uu.htm

(if you don't want the standard begin/end lines, it's probably a better idea
to use base64 encoding instead...)

</F>

Dec 9 '05 #7
py <co*******@gmail.com> wrote:
Alex Martelli wrote:
I suggest a redesign...!
What would you suggest? I have to encode/decode in chunks b/c of the
45 byte limitation.


Not quite:
s=45*'v'
a=binascii.b2a_uu(s)
len(a) 62 b=binascii.a2b_uu(a)
len(b) 45 b==s True


I.e., you can pass to a2b_uu ANY string (and ONLY such a string, not,
e.g., a slice or single char of it, as you're trying to do) that's a
result of a b2a_uu call; the length limitation applies only the other
way.

I join /F in suggesting yo use binascii the standard way, but, at any
rate, you should at least redesign your decoding strategy so it only
calls a2b_uu on strings which are the results of b2a_uu calls.
Alex
Dec 9 '05 #8
py
Thanks...I think base64 will work just fine...and doesnt seem to have
45 byte limitations, etc.

Thanks.

Dec 9 '05 #9
py <co*******@gmail.com> wrote:
Thanks...I think base64 will work just fine...and doesnt seem to have
45 byte limitations, etc.


Sure, base64 is a better encoding by all criteria, unless you
specifically need to use uu encoding for compatibility with other old
software.
Alex
Dec 10 '05 #10

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

Similar topics

5
2714
by: Juho Saarikko | last post by:
I made a Python script which takes Usenet message bodies from a database, decodes uuencoded contents and inserts them as Large Object into a PostGreSQL database. However, it appears that the to...
11
3733
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
117
7102
by: Peter Olcott | last post by:
www.halting-problem.com
28
5170
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
3785
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
4870
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
4534
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
9
3621
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
2
1574
by: Nelluru | last post by:
Hi, I am using PHP 5.2.5 and IIS 5.1 on Windows XP SP3 machine. I am trying to execute an exe by using exec or system command. When I run this php script from the command line it works fine...
0
7237
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
7349
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
7417
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...
1
7074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7506
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...
0
4734
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3210
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
445
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.