How do I make Python 3.4 c_char_array read strings as two bytes?

1.6k views Asked by At

I'm using pypyodbc with Python 3.4 on Ubuntu 12.04.

I'm trying to get the column names, but something is a little wonky. What is coming back is just the first character as a byte, like this:

(Pdb) Cname.value
b'T'

The thing behind the scenes is a ctypes char array:

(Pdb) Cname
<ctypes.c_char_Array_1024 object at 0xb6a1ad1c>

But if I look at the raw value:

(Pdb) Cname.raw
b'T\x00Y\x00P\x00E\x00_\x00N\x00A\x00M\x00E\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

you can see that the value TYPE_NAME is separated by \x00.

So it appears to me that what's happening is something (ctypes?) is reading that first \x00 as the null terminator for the string instead of part of the characters.

What can I do to modify the way ctypes is being used so that it will read the entire string? Everything else seems to work fine, it's just the descriptions that are wonky.

1

There are 1 answers

2
Kevin On BEST ANSWER

Your string is encoded in UTF-16LE. You want to call something like Cname.raw.decode('utf_16le').rstrip('\x00'). That will return a Python string, which you can then do with as you please.