void MainWindow::writeLog( char prefix, char data ){ if( LogFileName != "" ) { QFile log( LogFileName ); if( ! log.open( QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text ) ) return; QTextStream out( &log ); out.setNumberFlags( QTextStream::ShowBase | QTextStream::UppercaseDigits ); out.setIntegerBase( 16 ); out << prefix; out << (int) data; out << "\n"; log.close(); }}
C++ (Qt)void MainWindow::writeLog( char prefix, char data ){ if( LogFileName != "" ) { QFile log( LogFileName ); if( ! log.open( QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text ) ) return; QTextStream out( &log ); out << QString("%0%1").arg(prefix).arg(data, 2, 16, QChar('0')); log.close(); }}
C++ (Qt)while(!asleep()) sheep++;
C++ (Qt)#include <QtCore/qmath.h> #define BITS_PER_BYTE 8#define C_LN2 0.693147180559945309417 template <typename TNumberType>static QString format(TNumberType n, int base = 10){ int bitsPerDigit = qLn(static_cast<qreal>(base)) / C_LN2; int digits = sizeof(n) * BITS_PER_BYTE / bitsPerDigit; QString result = QString::number(n, base); result.prepend(QString(digits - result.length(), '0')); return result;} int main(int argc, char *argv[]){ qDebug() << format(10, 16); return 0;}