views:

36

answers:

1
+2  A: 

The first argument needs to be a byte string as well:

msvcrt.printf(b"%s", string)

The return value of printf is the number of characters printed, which should be 12 in this case.

Edit:

If you want the string to be returned instead of printed, you can use sprintf instead. This is dangerous and NOT recommended.

s = ctypes.create_string_buffer(100)   #must be large enough!!
msvcrt.sprintf(s, b'%s', b'Hello World!')
val = s.value

I don't know why you'd want to do this though, since Python has its own string formatting. sprintf is a dangerous method since it is susceptible to buffer overflows.

interjay
thanks for the quick reply, i tried that after u suggested it, i got the value 12 now instead of 1. print(msvcrt.printf(b"%s", string))
dsaccount1
12 is the correct result.
interjay
how would i get printf to return "Hello World!" instead of 12? is that the length?
dsaccount1
Thanks, i was looking thru someones code and didnt understand. Whats stopping someone from using printf instead of sprintf?Edit:I tried it out i got b''
dsaccount1
They do different things: printf prints a string to standard output, and sprintf returns the string.
interjay
So if i run the script with printf in a cmd prompt the script would spit out the string to the terminal?
dsaccount1
Yes.__________________
interjay
aight thanks for your patience!
dsaccount1