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. My Qt function works as not expected [solved]
Forum Updated to NodeBB v4.3 + New Features

My Qt function works as not expected [solved]

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 2.0k 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.
  • C Offline
    C Offline
    Cancer
    wrote on last edited by
    #1

    Hi!

    I have to convert some symbols have been read from textInput to hex string.
    Where I was debugging, I use that code in formData() method - it works good:
    @void MainWindow::formData(){
    QByteArray text_data = ui->lineEdit->text().toLocal8Bit(), data_to_write;
    char buf[2];
    unsigned int mByte;
    for(int pos = 0, i=0; pos<text_data.length(); pos+=2, i++){
    buf[0]=text_data[pos];
    buf[1]=text_data[pos+1];
    sscanf(buf, "%x", &mByte);
    data_to_write[i] = mByte;
    };
    emit formed(data_to_write);
    }@

    My sscanf(buf, "%x", &mByte); fragment for buf={'F', 'E'} returns to mByte 254 value as I expected.

    But when I wrote method to do this from a lot of places of my program - it returns 4068 value. Here is code of my methods:

    @void MainWindow::formData(){
    QByteArray text_data = ui->lineEdit->text().toLocal8Bit(), data_to_write;
    data_to_write = convertToHexArray(text_data);
    emit formed(data_to_write);
    }

    QByteArray MainWindow::convertToHexArray(QByteArray input){
    QByteArray data_to_write;
    char buf[2];
    unsigned int mByte;
    for(int pos = 0, i=0; pos<input.length(); pos+=2, i++){
    buf[0]=input[pos];
    buf[1]=input[pos+1];
    sscanf(buf, "%x", &mByte);
    data_to_write[i] = mByte;
    };
    return data_to_write;
    }@

    So, in second occurence sscanf returns 4068. Debugger shows that buf variable is same in both occasions.
    Why???

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Maybe a silly question but why don't you just use "QByteArray::toHex()":http://qt-project.org/doc/qt-5/qbytearray.html#toHex ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Cancer
        wrote on last edited by
        #3

        Because it converts each symbol in hex (e.g. 'f', 'e' will equal '46', '45'), but I want to write 254, as 1 byte, as 0xfe.

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Hi,
          Copy the bytes you need into a local ByteArray and then to the toHex. That should give you the total value in hex, not hex for every byte.

          Greetz, Jeroen

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Cancer
            wrote on last edited by
            #5

            [quote author="Jeroentje@home" date="1395403157"]Hi,
            Copy the bytes you need into a local ByteArray and then to the toHex. That should give you the total value in hex, not hex for every byte.[/quote]

            @QByteArray test = QByteArray("FE");
            qDebug()<<test.toHex();@

            returns 4645
            For each symbol. Or I misunderstood you?

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Cancer
              wrote on last edited by
              #6

              @QByteArray test = QByteArray::fromHex("FE002101");@

              Works as I planned. Let's try

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Cancer
                wrote on last edited by
                #7

                OKay, problem is solved.

                @void MainWindow::formData(){
                QByteArray text = ui->lineEdit->text().toLocal8Bit();
                QByteArray data_to_write = convertToHexArray(text);
                ...
                @

                @QByteArray MainWindow::convertToHexArray(QByteArray input){
                QByteArray data_to_write = QByteArray::fromHex(input);
                return data_to_write;
                }@

                1 Reply Last reply
                0
                • EddyE Offline
                  EddyE Offline
                  Eddy
                  wrote on last edited by
                  #8

                  Thanks for sharing your solution.

                  Also, could you edit the title of your first post an add [solved]
                  This way helpfull minds know it 's already solved;-)

                  Qt Certified Specialist
                  www.edalsolutions.be

                  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