python swig string to char* conversion

621 views Asked by At

I am working with a project that uses python SWIG module. The module has a function:

void func(char* pin, unsigned long len);

and is called from python like this:

key = "\x42\x1d\xd7"
modul.func(key)

The strange thing is that the size of incoming array is 4 instead of 3. The last character (d7) is being converted into two characters (c3 97). I noticed that if the byte has highest bit set, then it expands in two bytes. I suppose this has something to do with string encodings or such.

Is there another way of passing "\x42\x1d\xd7" to SWIG module function?

In another words: I need a way to create a string in python, which is kind of malformed. It needs to have only 3 bytes [0x42, 0x1d, 0xd7]

1

There are 1 answers

6
Anand S Kumar On

Do you mean to say you want to pass \x42\x1d\xd7 without it being converted into unicode?

The issue in your case comes because \x42\x1d\xd7 is converted into 'utf-8' when passing to modul.func() .

You can test this by doing -

key = "\x42\x1d\xd7"
key.encode('utf-8').decode('unicode-escape')
>> 'B\x1d\xc3\x97'

For your issue, you can stop the encoding/decoding of the key variable by python, by appending a r to the definition of the string as below -

key = r"\x42\x1d\xd7"

This tells the python interpreter to read the string raw . Try that.