Results 1 to 7 of 7

Thread: how can I pass a QString to vsnprintf?

  1. #1
    Join Date
    Jan 2006
    Posts
    55
    Thanks
    13
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default 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!

  2. #2
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how can I pass a QString to vsnprintf?

    Use the ascii() function
    Qt Code:
    1. vsnprintf(buffer, 1000-1, "%s", s.ascii() );
    To copy to clipboard, switch view to plain text mode 
    We can't solve problems by using the same kind of thinking we used when we created them

  3. #3
    Join Date
    Jan 2006
    Posts
    55
    Thanks
    13
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default 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().

  4. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how can I pass a QString to vsnprintf?

    You're using vsnprintf wrong:
    http://kernelnewbies.org/documents/k...-api/r889.html

    And why don't you use QString::sprintf()?

  5. #5
    Join Date
    Jan 2006
    Posts
    55
    Thanks
    13
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default 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.

  6. #6
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how can I pass a QString to vsnprintf?

    I generally use strcpy() for copying a qstring to a char array:
    Qt Code:
    1. strcpy(buuffer,fooQString.ascii())
    To copy to clipboard, switch view to plain text mode 
    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)

  7. #7
    Join Date
    Jan 2006
    Posts
    55
    Thanks
    13
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default 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!
    Last edited by coralbird; 18th January 2006 at 09:34.

Similar Threads

  1. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 17:26
  2. easiest Way QString can do
    By baray98 in forum Qt Programming
    Replies: 12
    Last Post: 15th April 2008, 21:49
  3. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 18:59
  4. How to pass a QString to another class ?
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 9th December 2006, 21:16
  5. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 23:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.