Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Quick Question Regarding Conversions in Qt [SOLVED]
Forum Updated to NodeBB v4.3 + New Features

Quick Question Regarding Conversions in Qt [SOLVED]

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vezprog
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      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.

      Nokia Certified Qt Specialist
      Interested in hearing about Qt related work

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

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

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved