Help to port c# to Qt c++



  • Hello, i m beginner in Qt c++ and i try to port a c# programm to Qt c++.
    I don't know how to do that:

    in c#:
    byte[] t = System.Text.Encoding.UTF8.GetBytes(trame); // trame is a string

    in Qt c++:
    ?

    Can anyone help me please?


  • Moderators

    @iTof26

    char[] t= QString(trame).toUtf8().constData()
    

  • Lifetime Qt Champion

    Hi,

    Since you are likely going to use t somewhere else, rather:

    QByteArray trameBytes = QString(trame).toUtf8();
    char[] t = trameBytes.constData();
    

    otherwise you might be pointing to a buffer that can get invalidated.



  • Ok, thank you for yours fast answers

    but when i try :
    QByteArray trameBytes = QString(trame).toUtf8();
    char t[] = trameBytes.constData();

    i can't compile:
    initializer fails to determine size of 't"
    array must be initialised wit a brace-enclosed initialiser

    i don't know how to determine the size because 'trame' is variable


  • Lifetime Qt Champion

    Depending on what you are going to do with it, use a const char *.

    In fact, what do you need t for ?



  • it 's for sending with QTcpsocket

    QByteArray trameBytes = QString(trame).toUtf8();
    const char* t[] = trameBytes.constData();

    clientSocket->write(...........

    but i have error when i compile


  • Qt Champions 2017

    Hi
    The [] you are using is not 100% correct for c++. Its a C# syntax, i think. ( at least when used like that)
    Why not use constData() directly ?
    clientSocket->write(trameBytes.constData(), xxx

    or
    const char* t = trameBytes.constData();

    Also if socket is Qt version, i think it can take ByteArray directly.


  • Lifetime Qt Champion

    No need for all that stuff then.

    clientSocket->write(frameBytes);

    is enough.


  • Qt Champions 2017

    @mrjj said in Help to port c# to Qt c++:

    The [] you are using is not 100% correct for c++. Its a C# syntax, i think. ( at least when used like that)

    You're correct, and our fellows fell into that one head first. :)

    char t[] is a valid construction only in two cases:

    1. If the size of t can be determined at compile-time, e.g. char t[] = { 'a', 'b' };.
    2. It's a formal parameter for a function, then char t[] equates to char * const t.
      Note: When passing multidimensional arrays as formal parameters only the first dimension can be left without explicitly specifying the size.

  • Moderators

    @kshegunov said in Help to port c# to Qt c++:

    You're correct, and our fellows fell into that one head first. :)

    yep, my fault. Answered too fast.


  • Qt Champions 2017

    @raven-worx, I wasn't looking to place blame, I just found it somewhat funny (no offence). :D


  • Moderators

    @kshegunov
    i didn't take it as such anyway.
    No piece of code exists without bugs ;)



  • Perfect, finaly i use:

    QByteArray trameBytes = QString(trame).toUtf8();
    clientSocket->write(trameBytes.constData());

    Thanks all for your help :)


  • Lifetime Qt Champion

    Again: there's no need to call constData. QTcpSocket has a write method that takes a QByteArray.



  • @SGaist said in Help to port c# to Qt c++:

    No need for all that stuff then.

    clientSocket->write(frameBytes);

    is enough.

    As a general rule of thumb, when you use Qt, use QString and QByteArray as early as you can and use char*-arrays as late as possible. If you create a string with "", create a QString: QString("a string you know compile time"). If you get [] array data from outside Qt program, convert it as soon as you get it. Most Qt classes/functions which can use char* directly have that possibility only for convenience: for example QTcpSocket and other classes which communicate with outside world take and give QByteArrays. Low level stuff or c++ standard library stuff is of course sometimes necessary or preferable (there might be several good reasons), but don't use it unless you have a specific reason. See docs for QString, QByteArray and QIODevice. They are all important basic classes in Qt programming. And of course the container classes like QList.


  • Qt Champions 2017

    With the minor note you should use QStringLiteral("some literal") or QLatin1Literal("latin 1 literal") to enable moc to do a bit of magic and optimize some constructor calls.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.