Deepal
Deepal

Reputation: 1814

Using C datatypes with ctypes in python

I tried to use C char pointer datatype in python 3.3. I used following code:

    from ctypes import *

    firstname = c_char_p("I am a noob programmer".encode("utf-8"))
    print(firstname.value)

My desired output was

    I am a noob programmer

but what I got was

    b'I am a noob programmer'

I am following a tutorial made for python 2.6. I used "ascii" as the parameter for encode function. And also tried the "bytes()" instead of encode. But no difference at either time.Why don't I get my desired output? and How can I get the output as desired.

Please anybody help me to understand..

Upvotes: 0

Views: 171

Answers (1)

Janne Karila
Janne Karila

Reputation: 25197

In Python 3 you need to decode it back to str if you want it to behave like str.

print(firstname.value.decode("utf-8"))

Upvotes: 1

Related Questions