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]
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 tomodul.func()
.You can test this by doing -
For your issue, you can stop the encoding/decoding of the
key
variable by python, by appending ar
to the definition of the string as below -This tells the python interpreter to read the string
raw
. Try that.