Cython convert python array to vector[char]

149 views Asked by At

In python, I make a call to a calculate function inn my pyx file, which then calls other cdef functions.

python function:

def getCoord():
   array = ['A','B','C','D']
   px = 5
   py = 6
   move = pyxFile.calculateCoord( array, x, y )

pyx function:

def calculateCoord( array, px, py):
   cdef vector[ char ] b

   for i in range( len( array) ):
      b.push_back( array[ i ] )

   return search( b, px, py )

I'm trying to pass a python array of characters to a vector[char] for use in my cdef functions, but my error is an integer is required on the line b.push_back( array[ i ] )

x and y are integers

search (a cdef function) returns a tuple of integers

1

There are 1 answers

0
DavidW On BEST ANSWER

Your inputs are length-1 strings rather than chars. I know this seems like it should be identical, but it isn't.

You can use ord to get the integer value of a length-1 string:

  b.push_back( ord(array[ i ]) )