469,306 Members | 1,896 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Working with bytes.

I have been unable to solve a problem. I am working with MD5 signatures
trying to put these in a database. The MD5 signatures are not generated
using the python md5 module but an external application that is
producing the valid 16 byte signature formats. Anyway, these 16 byte
signatures are not nescarrally valid strings. How do I manipulate the
bytes? I need to concatenate the bytes with a SQL statement which is a
string. This works fine for most of the md5 signatures but some blow up
with a TypeError. Because there is a NULL byte or something else. So I
guess my ultimate question is how do I get a prepared SQL statement to
accept a series of bytes? How do I convert the bytes to a valid string
like:

'x%L9d\340\316\262\363\037\311\345<\262\357\215'

that can be concatenated?

Thanks

Jul 18 '05 #1
2 2129
Adam T. Gautier wrote:
I have been unable to solve a problem. I am working with MD5 signatures
trying to put these in a database. The MD5 signatures are not generated
using the python md5 module but an external application that is
producing the valid 16 byte signature formats. Anyway, these 16 byte
signatures are not nescarrally valid strings. How do I manipulate the
bytes? I need to concatenate the bytes with a SQL statement which is a
string. This works fine for most of the md5 signatures but some blow up
with a TypeError. Because there is a NULL byte or something else. So I
guess my ultimate question is how do I get a prepared SQL statement to
accept a series of bytes? How do I convert the bytes to a valid string
like:

'x%L9d\340\316\262\363\037\311\345<\262\357\215'

that can be concatenated?

Thanks

This should probably do:
cursor.execute('SELECT author from stored where md5 = ?', md5)

But you might consider changing your approach to store hex strings:
cursor.execute('SELECT author from stored where md5 = ?',
''.join(['%02x' % byte for byte in md5]))
--
-Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #2
>>>>> "Adam T. Gautier" <ad**********@yahoo.com> (ATG) wrote:

ATG> I have been unable to solve a problem. I am working with MD5 signatures
ATG> trying to put these in a database. The MD5 signatures are not generated
ATG> using the python md5 module but an external application that is producing
ATG> the valid 16 byte signature formats. Anyway, these 16 byte signatures are
ATG> not nescarrally valid strings. How do I manipulate the bytes? I need to
ATG> concatenate the bytes with a SQL statement which is a string. This works
ATG> fine for most of the md5 signatures but some blow up with a TypeError.
ATG> Because there is a NULL byte or something else. So I guess my ultimate
ATG> question is how do I get a prepared SQL statement to accept a series of
ATG> bytes? How do I convert the bytes to a valid string like:

ATG> 'x%L9d\340\316\262\363\037\311\345<\262\357\215'

ATG> that can be concatenated?

Depends what the database accepts. If it supports BLOBs you could use
that. Otherwise convert them to hex, or base64.
See for example the the codecs module or the binascii module.

--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.***********@hccnet.nl
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Larry Menard | last post: by
7 posts views Thread by Alex | last post: by
8 posts views Thread by den 2005 | last post: by
4 posts views Thread by SpreadTooThin | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.