zoukankan      html  css  js  c++  java
  • QT 获取文件MD5值

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. /* 方法1 */  
    2.     QFile theFile(fileNamePath);  
    3.     theFile.open(QIODevice::ReadOnly);  
    4.     QByteArray ba = QCryptographicHash::hash(theFile.readAll(), QCryptographicHash::Md5);  
    5.     theFile.close();  
    6.     qDebug() << ba.toHex().constData();  
    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. /* 方法2 */  
    2. /* 
    3. *   获取文件md5值 
    4. */  
    5. QByteArray MainWindow::getFileMd5(QString filePath)  
    6. {  
    7.     QFile localFile(filePath);  
    8.   
    9.     if (!localFile.open(QFile::ReadOnly))  
    10.     {  
    11.         qDebug() << "file open error.";  
    12.         return 0;  
    13.     }  
    14.   
    15.     QCryptographicHash ch(QCryptographicHash::Md5);  
    16.   
    17.     quint64 totalBytes = 0;  
    18.     quint64 bytesWritten = 0;  
    19.     quint64 bytesToWrite = 0;  
    20.     quint64 loadSize = 1024 * 4;  
    21.     QByteArray buf;  
    22.   
    23.     totalBytes = localFile.size();  
    24.     bytesToWrite = totalBytes;  
    25.   
    26.     while (1)  
    27.     {  
    28.         if(bytesToWrite > 0)  
    29.         {  
    30.             buf = localFile.read(qMin(bytesToWrite, loadSize));  
    31.             ch.addData(buf);  
    32.             bytesWritten += buf.length();  
    33.             bytesToWrite -= buf.length();  
    34.             buf.resize(0);  
    35.         }  
    36.         else  
    37.         {  
    38.             break;  
    39.         }  
    40.   
    41.         if(bytesWritten == totalBytes)  
    42.         {  
    43.             break;  
    44.         }  
    45.     }  
    46.   
    47.     localFile.close();  
    48.     QByteArray md5 = ch.result();  
    49.   
    50.     return md5;  
    51. }  

    http://blog.csdn.net/emdfans/article/details/23871741

  • 相关阅读:
    elform 校验
    深入理解ES6系列
    【数据结构&算法】10串基础&KMP算法源码
    【数据结构&算法】13赫夫曼树&赫夫曼编码
    【RTOS】FreeRTOS中的任务堆栈溢出检测机制
    【数据结构&算法】11树基础&二叉树遍历
    【环境】解决linux与windows之间的复制粘贴
    【数据结构&算法】09队列概念&参考源码
    【网络基础】内网IP与外网IP
    【数据结构&算法】12线索二叉树
  • 原文地址:https://www.cnblogs.com/findumars/p/5599439.html
Copyright © 2011-2022 走看看