how can I pass a QString to vsnprintf?
QString str("this is a test");
char buffer[1000];
memset(buffer,0,1000);
vsnprintf(buffer,1000-1,"%s",s);
the compiler tells:invalid conversion from 'const char*' to "char*'
In mfc, I can use CString itself or CString.GetBuffer() to do this.
Thanks a lot!
Re: how can I pass a QString to vsnprintf?
Use the ascii() function
Code:
vsnprintf(buffer, 1000-1, "%s", s.ascii() );
Re: how can I pass a QString to vsnprintf?
QString.ascii() return const char *,not char *, so if use
vsnprintf(buffer, 1000-1, "%s", s.ascii() )
the compiler report a error: invalid conversion from `const char*' to `char*'
So i have to convert as below,
vsnprintf(buffer, 1000-1, "%s", (char*)s.ascii() )
and there is no error when compiling and linking. but the program
crashed when run to this code .the same happeded when i use latin1().
Re: how can I pass a QString to vsnprintf?
Re: how can I pass a QString to vsnprintf?
I have to pass a struct from server to client.every memberof this struct is
a fixed char array.As a part of communication protocol,fixed char array is necessary.
So i must put data into the struct from QString.
Re: how can I pass a QString to vsnprintf?
I generally use strcpy() for copying a qstring to a char array:
Code:
strcpy(buuffer,fooQString.ascii())
You could also pass the QString ascii (or utf8 or whatever) encoded data to a QByteArray and simply memcpy them. The printf family has proven itself pretty unusable for handling QStrings (at least for me)
Re: how can I pass a QString to vsnprintf?
maybe this is a way:
first use QString.sprintf() to format the target,then use strncpy or strcpy to copy it to
destination.
thanks to all!