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. Convert a QDate to BCD Format (Binary-coded decimal) for QByteArray
Forum Updated to NodeBB v4.3 + New Features

Convert a QDate to BCD Format (Binary-coded decimal) for QByteArray

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 934 Views 2 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.
  • P Offline
    P Offline
    Pantoufle
    wrote on last edited by
    #1

    Hello,

    I don't know if this is the good place to post this, as it's a bit of a c++ question, but as it involve QDate and QByteArray i guess it's somehow related to Qt :p .

    For a project i have to convert a date into a block of bytes in BCD Format.
    So logically I used QDate for date and QbyteArray for my bytes block.
    I managed to do it but I think my code is not optimizing at all and that there is a much easier and cleaner way to do it.

    What i want to do :

    Imagine we are the november 17 of year 2021 (randomly :p), i want the QByteArray to be like this :

    " 0x20 0x21 0x11 0x17 "

    Here is how I currently do :

            QByteArray data;
            QDate date = QDate::currentDate();
            QString date_string = date.toString("yyyyMMdd");
            char tmp = 0;
            QString current_char;
            int i = 0;
        
            while(i < date_string.length())
            {
                current_char = date_string.at(i);
                tmp |= static_cast<char>(current_char.toInt()) << 4;
                i++;
                current_char = date_string.at(i);
                tmp |= static_cast<char>(current_char.toInt());
                i++;
        
                data += tmp;
                tmp = 0;
            }
        
            qDebug() << data.toHex();
    

    The code works, for what I tested, but I feel it's not the right way to do it

    If someone can learn me how to do it gracefully I would be pleased.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mpergand
      wrote on last edited by mpergand
      #2

      It's all about hex conversion:

        QDate date = QDate(1935,3,30);
        QString date_string = date.toString("yyyyMMdd");
        QByteArray data=QByteArray(date_string.toLocal8Bit());
        QByteArray bcd=QByteArray::fromHex(data);
        
        qDebug()<<date_string<<bcd.toHex();
      
      1 Reply Last reply
      1
      • P Offline
        P Offline
        Pantoufle
        wrote on last edited by
        #3

        Wow ok, I expected my code to be too complicated. But not at this point ^^.

        Thanks and have a nice day.

        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