1

I'm using PyCrypto for generating secure key hashes. I want to store one or more of the partial keys I generate. Each partial key is in the form

\x0f|4\xcc\x02b\xc3\xf8\xb0\xd8\xfc\xd4\x90VE\xf2

I have an ndb StringProperty() in which I'd lke to store that info. However, it raises a BadValueError saying it expects an UTF-8 encoded string. I tried using str's .encode('uft-8') method but that also raises an error telling me it couldn't encode because bad positioning.

Anyway, my question is, how can I convert that byte string into something I can store in ndb?

Lipis
  • 21,388
  • 20
  • 94
  • 121
rdodev
  • 3,164
  • 3
  • 26
  • 34

1 Answers1

2

Improved Answer:

In this case instead of storing the key as String or Text, you should use a BlobProperty which stores an uninterpreted byte string.

Original Answer:

To convert bytes (strings) to unicode you use the method decode. You also need to use an encoding that preserves the original binary data, which is ISO-8859-1. See ISO-8859-1 encoding and binary data preservation

unicode_key = key.decode('iso-8859-1')
bytes_key = unicode_key.encode('iso-8859-1')

Consider also using A TextProperty instead, as StringProperties are indexed.

Community
  • 1
  • 1
Sebastian Kreft
  • 7,819
  • 3
  • 24
  • 41
  • You would need to do unicode_key.encode('iso-8859-1') to get the original bytes. After some thought in this case the best option is to use a BlobProperty instead. See update. – Sebastian Kreft Apr 25 '13 at 17:42
  • Thanks. I ended up using TextProperty since BlobProperty was expecting str and decoding and encoding to iso-8859-1 produced unicode – rdodev Apr 25 '13 at 18:41