Quick Question Regarding Conversions in Qt [SOLVED]

  • Bare in mind, I am sort of a newbie to C++ and Qt, So here is what I am trying to do. I cant seem to figure out the proper way to do this though. I am currently reading a udp socket into a char array "char updDataIn[103]"

    For certain purpose's, this absolutely needs to be a char. I have other code already functioning and working with the use of this variable. What I am trying to do here is convert the char into a long. Since it is a single char, some confusing has arose (not as easy as using atol()). I know I can use the toChar for Qt variables like QString so my thoughts were to do QChar of the char, and then make that a QString, and toLong on the postfix. Each previous calculation needs to be added to the current calculation since I am building a serial number string so output to a text box. Unfortunately, this doesn't seem to work, I was curious if anyone else had any other ideas on how to do this. I failed to find any easy ways with generic C++ to do this as well.

    Heres the code snipet:
    // *** build hardware serial number string ***
    serialNumber = QString(QChar(udpDataIn[61])).toLong(); // 1st part
    serialNumber = serialNumber + QString(QChar(udpDataIn[62]) << 8).toLong(); // 2nd part
    serialNumber = serialNumber + QString(QChar(udpDataIn[63]) << 16).toLong(); // 3rd part
    serialNumber = serialNumber + QString(QChar(udpDataIn[64]) << 24).toLong(); // 4th part
    ui->txtserialnumber->setText(QString(serialNumber)); // output to UI

    I get multiple errors when doing this due to it not liking the bit shifting process. Note: serialNumber is denoted as a Long.

    Edit: I fixed my issues, I added .toLong(); I still would like to know if their is an easier way to do this other than casting a million times. Thanks in advance!

  • Are you sure you have the bitshifts in the correct place in the code above? Looks as if you are trying to bitshift a QChar. You could use the QChar::digitValue() function to save yourself the hassle of going via QString since you are converting and shifting one digit at a time to build your id.

  • can you please show an example string that you get (in udpDataIn[61 - 64]) and show the expected result? It seems very difficult what you do there should be easier...

Log in to reply

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