The Unicode string '\xff'
is converted into the UTF-8 byte sequence b'\xc3\xbf'
exactly as you have coded.
If you want to send byte data, use a byte string (b'\xff\x0a\x82'
) and use a PyBytes
method to convert it to a char*
and size:
%typemap(in) (char* data,int size) (Py_ssize_t len) %{
if(PyBytes_AsStringAndSize($input,&$1,&len) == -1)
return NULL;
$2 = (int)len;
%}
Note that this should work on 32- and 64-bit Windows and null bytes can be included in the data, since the size is explicitly extracted and it handles the possibility of 64-bit Py_ssize_t
.