I want to print a C string using the Python print statement. The array vendorName contains the ASCIIZ C string A ANTHONY & SONS INC. My motivation is to convert the C string to a Python string such that I can use all of the Python string methods.
I have a struct:
class _vendrRecord(Structure):
_pack_ = 1 # pack the struct
_fields_ = [
("vendorName" ,c_ubyte *(40 + 1)),
("ytdPayments" ,c_ulong),
]
I want to print the string "vendorName" which is ASCIIZ.
I can print it using printf like this:
printf(b"%s\n", vendrRecord.vendorName)
I have tried this print(vendrRecord.vendorName) but it just prints the address. Based on information from Jamie Nicholl-Shelley, I tried print(cast(vendrRecord.vendorName,c_char_p).value) but that gives b'A ANTHONY & SONS INC'. I want simply A ANTHONY & SONS INC
Note that print(vendrRecord.ytdPayments) prints correctly.
vendrRecord.vendorName.content? I'm not so much into ctypes though.vendrRecord.vendorName.contentgivesAttributeError: 'c_ubyte_Array_41' object has no attribute 'content'