Thanks to @ErykSun the solution:
Python code
string1 = "my string 1"
string2 = "my string 2"
# create byte objects from the strings
b_string1 = string1.encode('utf-8')
b_string2 = string2.encode('utf-8')
# send strings to c function
my_c_function.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
my_c_function(b_string1, b_string2)
Answer from MorphieBlossom on Stack OverflowThanks to @ErykSun the solution:
Python code
string1 = "my string 1"
string2 = "my string 2"
# create byte objects from the strings
b_string1 = string1.encode('utf-8')
b_string2 = string2.encode('utf-8')
# send strings to c function
my_c_function.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
my_c_function(b_string1, b_string2)
I think you just need to use c_char_p() instead of create_string_buffer().
string1 = "my string 1"
string2 = "my string 2"
# create byte objects from the strings
b_string1 = string1.encode('utf-8')
b_string2 = string2.encode('utf-8')
# send strings to c function
my_c_function(ctypes.c_char_p(b_string1),
ctypes.c_char_p(b_string2))
If you need mutable strings then use create_string_buffer() and cast those to c_char_p using ctypes.cast().
ctypes.cast() is used to convert one ctype instance to another ctype datatype. You don't need it To convert it to python string. Just use ".value" to get it in python string.
>>> s = "Hello, World"
>>> c_s = c_char_p(s)
>>> print c_s
c_char_p('Hello, World')
>>> print c_s.value
Hello, World
More info here
If you set the argtypes or restype attributes of ctypes functions, they will return the right Python object without the need for a cast.
Here's an example calling the C-runtime time and ctime functions:
>>> from ctypes import *
>>> m=CDLL('msvcrt')
>>> t=c_long(0)
>>> m.time(byref(t))
1326700130
>>> m.ctime(byref(t)) # restype not set
6952984
>>> m.ctime.restype=c_char_p # set restype correctly
>>> m.ctime(byref(t))
'Sun Jan 15 23:48:50 2012\n'
Convert string to char in python - Stack Overflow
Dealing with char*** in ctypes
C char array from python string - Stack Overflow
How do I convert a string to an array of chars in C?
Videos
To answer the question without reading too much else into it I would
Copychar str[2] = "\0"; /* gives {\0, \0} */
str[0] = fgetc(fp);
You could use the second line in a loop with whatever other string operations you want to keep using chars as strings.
You could do many of the given answers, but if you just want to do it to be able to use it with strcpy, then you could do the following:
Copy...
strcpy( ... , (char[2]) { (char) c, '\0' } );
...
The (char[2]) { (char) c, '\0' } part will temporarily generate null-terminated string out of a character c.
This way you could avoid creating new variables for something that you already have in your hands, provided that you'll only need that single-character string just once.
No. Use a dictionary.
>>> names = {'p1': 100, 'p2': 200}
>>> t = names['p1']
>>> t
100
This will throw a KeyError if the name does not exist:
>>> t = names['p3']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'p3'
... but you can use dict.get and provide a default value:
>>> t = names.get('p3', 'default')
>>> t
'default'
where the default-default-value is None.
(By the way, this has nothing to do with converting strings to a char(acter?), your terminology is quite confusing.)
You're probably looking for eval(): https://docs.python.org/3.5/library/functions.html#eval
p1 = 100
print(eval('p1')) # prints 100
If you want to evaluate expressions you can also use exec()
p1 = 100
t = 'p1 += 99'
exec(t)
print(p1) # prints 199
But as other already pointed out, you should avoid it if possible. timgeb provided a nice alternative with dictionaries for example.
If you want to turn a string into a global variable you could also do:
globals()['y'] = 5
print(y)
Note: when you run your code in a global scope (outside a function), you can also use locals()['y'] = 5 - but this will not work in a non-global scope!
I know that a string is already technically an array of chars, but when I try to use toupper(string), it doesn’t work because toupper is designed to capitalize chars and not strings, per the documentation. I’ve been making it overly complicated and it’s stressing me out. So to start, I created an “int N=strlen(string);”, then created an array that’s “char upper[N];”. Then I write a for loop written as(please forgive the terrible syntax I’m about to write), “for (int i = 0; i < N; i++) { toupper(upper[j]); }”. What am I doing wrong?
I've found the solution
name = bytes(self.name.get().encode('utf-8'))
cmd.name = name
Here's a reproducible example. .encode() should be all that's needed:
from tkinter import *
from ctypes import *
class Demo(Structure):
_fields_ = [('name',c_char*20)]
root = Tk()
s = StringVar()
s.set('Hello, world!')
d = Demo()
d.name = s.get().encode()
print(d.name)
Output:
b'Hello, world!'