String issue when switching from python 2.x to python 3

I am having a problem with line feeds from python 2.x in python 3

Problem 1:

from ctypes import* charBuffer=create_string_buffer(1000) var = charBuffer.value # var contains like this "abc:def:ghi:1234" a,b,c,d= var.split(':') 

It works fine in python 2.x, but not 3.x, it throws some errors like this a, b, c, d = var.split (':') TypeError: 'str' does not support the buffer interface

I got links after some research on stackoverflow link link2

If I print, the desired result will be

 a= abc b =def c=ghi d=1234 

Issue2:

 from ctypes import* cdll = "Windll" var = 0x1fffffffffffffffffffffff # I want to send this long variable to character pointer which is in cdll charBuf =create_string_buffer(var.to_bytes(32,'little')) cdll.createBuff (charBuf ) cdll function int createBuff (char * charBuff){ print charBuff return 0; } 

I want to send this long variable to a character pointer, which is in the cdll, since its character indicates its throwing errors. You need your valuable materials on how I can achieve this. thanks in advance

+4
source share
2 answers

In Python 3.x, '.value' when returning create_string_buffer () returns a byte string.

In your example, you are trying to split a byte string using a Unicode string (which is a normal string in Python 3.x). This is what causes your problem.

You need to either split the byte string. Example -

 a,b,c,d = var.split(b':') 

Or you can decode a byte string to a Unicode string using the ".decode ()" method.

Example -

 var = var.decode('<encoding>') 
+2
source

Separate with b":" and everything will be fine in both versions of python.

In py2 str is a byte string, in py3 str is a unicode object. The object returned by the ctypes string buffer is a byte ( str on py2 and bytes on py3). By writing a string literal as b"... , you force it to be byte in both versions of python.

0
source

All Articles