1

I am using python v2.7.3 and am trying to get a conversion to work but am having some issues.

This is code that works the way I would like it to:

testString = "\x00\x13\xA2\x00\x40\xAA\x15\x47"
print 'Test String:',testString 

This produces the following result

TestString: ¢@ªG

Now I load the same string as above along with some other data:

\x00\x13\xA2\x00\x40\xAA\x15\x47123456

into a SQLite3 database and then pull it from the database as such:

cur.execute('select datafield from databasetable')
rows = cur.fetchall()
if len(rows) == 0:
    print 'Sorry Found Nothing!'
else:
    print row[0][:32]

This however produces the following result:

\x00\x13\xA2\x00\x40\xAA\x15\x47

I can not figure out how to convert the database stored string to the bytes string, if that is what it is, as the first snippet of code does. I actually need it to load into a variable in that format so I can pass it to a function for further processing.

The following I have tried:

print "My Addy:",bytes(row[0][:32])
print '{0}'.format(row[0][:32]) 
...

They all produce the same results...

Please

First, Can anyone tell me what format the first results are in? I think its bytes format but am not sure.

Second, How can I convert the database stored text into

Any help and I would be eternally grateful.

Thanks in advance,

Ed

tomlogic
  • 11,489
  • 3
  • 33
  • 59
Edd
  • 85
  • 2
  • 8
  • 1
    You say you're on 3.x, but you're using 2.x print syntax. Are you sure you know what version you're using? – user2357112 Jun 02 '14 at 20:41
  • possible duplicate of [Best way to convert string to bytes in Python 3?](http://stackoverflow.com/questions/7585435/best-way-to-convert-string-to-bytes-in-python-3) – MxLDevs Jun 02 '14 at 20:47
  • This code doesn't produce the output you say it does on Python 3. It produces a SyntaxError. – user2357112 Jun 02 '14 at 20:50
  • Hi, I checked and you are right. I am running version 2.7.3. My sincerest apologies for the confusion. – Edd Jun 02 '14 at 21:15
  • Hi MxyL, Thanks for the update, however, its not a duplicate at least I believe because I saw that post, tried it and it didn't work for me. Thanks for the info! – Edd Jun 02 '14 at 21:17
  • 1
    Please print out the `repr()` value of the string you want to convert. – poke Jun 02 '14 at 21:35
  • What encoding? UTF-8 bytes won't be the same as BE UTF-16 bytes. – David Ehrmann Jun 02 '14 at 21:48
  • Thanks for the update, I did the following print repr(bytes(row[0][:32]) and it produced u'\\x00\\x13\\xA2\\x00\\x40\\xAA\\x15\\x47'. – Edd Jun 02 '14 at 22:19
  • is there a way to convert this to a string as in the first part of my question? – Edd Jun 03 '14 at 00:34

1 Answers1

1

The problem is that you're not storing the value in the database properly. You want to store a sequence of bytes, but you're storing an escaped version of those bytes instead.

When entering string literals into a programming language, you can use escape codes in your source code to access non-printing characters. That's what you've done in your first example:

testString = "\x00\x13\xA2\x00\x40\xAA\x15\x47"
print 'Test String:',testString 

But this is processing done by the Python interpreter as it's reading through your program and executing it.

Change the database column to a binary blob instead of a string, then go back to the code you're using to store the bytes in SQLite3, and have it store the actual bytes ('ABC', 3 bytes) instead of an escaped string ('\x41\x42\x43', 12 characters).

If you truly need to store the escaped string in SQLite3 and convert it at run-time, you might be able to use ast.literal_eval() to evaluate it as a string literal.

tomlogic
  • 11,489
  • 3
  • 33
  • 59