Results 1 to 5 of 5

Thread: convert from QString to char[sizeof(...)]

  1. #1
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default convert from QString to char[sizeof(...)]

    Hello.

    I want to convert from QString to char[sizeof(...)] and vice versa. I was checking some threads in the forum but I can convert to char* and not to char[sizeof(..)], I know is almost the same but when I write the following code:

    QString string;
    char msg[sizeof(SIZE)];
    QByteArray ba = string.toLatin1();

    const char *c_str2 = ba.data();
    msg=(char*)c_str2;

    I obtain this error: incompatible types in assignment of `char*' to `char[2]'

    I can imagine that it is something basic but I dont know how to do it.

    P.d. I need use [sizeof(..)] to assign a determinate memory space.
    P.d. I was checking this thread to check how convet a QString to char* http://www.qtsoftware.com/developer/...30.9032238253/

    Thank you.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: convert from QString to char[sizeof(...)]

    char * is a pointer to some characters somewhere in your memory.
    char[] is a block of memory containing characters.

    Different concepts really.
    If you want to "assign" the pointer to the block, you need to copy the block pointed to by the pointer: this is what strcpy (and strncpy) are for.

    Qt Code:
    1. QString string;
    2. // make sure we do not overwrite "msg" (ie prevent writing more bytes than the buffer can accomodate)
    3. // (there are more elaborate ways to do that)
    4. // note esp. that strncpy does [B]not[/B] guarantee the result to be 0-terminated when the input is too large!
    5. strncpy(msg, string.toLatin1().constData(), sizeof(msg));
    6. // therefore, often one does
    7. msg[sizeof(msg)-1]=0;
    To copy to clipboard, switch view to plain text mode 

    Note that if you need to pass a char* to a C-API you can just pass string.toLatin1().constData() ... the pointer will be til its surrounding expression has been evaluated:
    Qt Code:
    1. some_c_api(... string.toLatin1().constData(), ...);
    To copy to clipboard, switch view to plain text mode 

    However, this would be very bad
    Qt Code:
    1. const char* msg = string.toLatin1().constData();
    2. some_c_api(... msg, ...); // msg is dangling pointer now!
    To copy to clipboard, switch view to plain text mode 
    HTH

  3. The following user says thank you to caduel for this useful post:

    adamatic (3rd February 2009)

  4. #3
    Join Date
    Jan 2009
    Posts
    54
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: convert from QString to char[sizeof(...)]

    ok, thank you.

    I have used the strncpy function and it is working now.

    I gave more space to the msg too becuase I was losing datas sometimes becuase the string it was bigger than the msg.

    Regards.

  5. #4
    Join Date
    Dec 2006
    Posts
    160
    Thanks
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: convert from QString to char[sizeof(...)]

    Bump. i'm confused about something.
    Quote Originally Posted by caduel View Post
    Qt Code:
    1. some_c_api(... string.toLatin1().constData(), ...);
    To copy to clipboard, switch view to plain text mode 
    When passing the string.toLatin1().constData() to some_c_api, while inside some_c_api isn't the QByteArray generated by string.toLatin1() already destructed?

    i myself do:
    Qt Code:
    1. QByteArray arr = theString.toLatin1();
    2. some_c_api(arr.constData());
    To copy to clipboard, switch view to plain text mode 

    Isn't it risked to do the first method?

    Thanks,
    Pierre.

  6. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: convert from QString to char[sizeof(...)]

    The scope of temporary objects is the end of the statement (i.e. the semi-colon). The temporary QByteArray is valid until the end of the some_c_api() function call.

    Something like this would be risky:
    Qt Code:
    1. const char *s = theString.toLatin1().constData();
    2. // the byte array disappears here but you are holding a pointer to some of its deallocated memory
    3. some_c_api(s);
    4. // potential boom
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. convert QString to QByteArray
    By morgana in forum Qt Programming
    Replies: 5
    Last Post: 2nd March 2011, 13:33
  2. Read binary file and convert to QString
    By jaca in forum Qt Programming
    Replies: 12
    Last Post: 13th June 2008, 23:05
  3. convert QString to int
    By mattia in forum Newbie
    Replies: 2
    Last Post: 4th January 2008, 09:10
  4. how to convert int to QString?
    By phillip_Qt in forum Newbie
    Replies: 2
    Last Post: 5th October 2007, 08:07
  5. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59

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.