Convert a QDate to BCD Format (Binary-coded decimal) for QByteArray
-
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.
-