zoukankan      html  css  js  c++  java
  • QT 读写二进制 (数值)高位在前

    在人们的计数规则中,一般都认为高位在前,即往前的地位大,如123,我们认为是一百二十三,

    但在计算机中数值是以二进制存储的,字节是最小的存储单位,如int(32位),占4个字节,每个字节有八位,

    24用十六进制表示,高位在前  是00000018,但如果以高位在后,则为18000000,

    看出高位在前和高位在后,是说四个字节的顺序,而每个字节中的二进制,一定是以高位在前排列的,

    见笔记本

    QT中是怎样处理高位在前、高位在后的:

    void QDataStream::setByteOrder(ByteOrder bo)

    {
    byteorder = bo;
    if (QSysInfo::ByteOrder == QSysInfo::BigEndian)
    noswap = (byteorder == BigEndian);
    else
    noswap = (byteorder == LittleEndian);
    }
     
    QDataStream &QDataStream::operator>>(qint32 &i)
    {
    i = 0;
    CHECK_STREAM_PRECOND(*this)
    if (noswap) {
    if (dev->read((char *)&i, 4) != 4) {
    i = 0;
    setStatus(ReadPastEnd);
    }
    } else { // swap bytes
    union {
    qint32 val1;
    char val2[4];
    } x;
    char *p = x.val2;
    char b[4];
    if (dev->read(b, 4) == 4) {
    *p++ = b[3];
    *p++ = b[2];
    *p++ = b[1];
    *p = b[0];
    i = x.val1;
    } else {
    setStatus(ReadPastEnd);
    }
    }
    return *this;
    }
    
    

    http://blog.sina.com.cn/s/blog_a401a1ea0101fjtx.html
  • 相关阅读:
    算法分析实验题集
    程序猿怎样解除烦恼
    MYSQL设计优化
    模式匹配KMP
    ios创建画笔的样例(双笔画效果)
    命令行解析器
    作业还是作孽?——Leo鉴书79
    客户机增加域 及server文件共享
    MySQL教程及经常使用命令1.1
    jsTree插件简介(三)
  • 原文地址:https://www.cnblogs.com/findumars/p/4735312.html
Copyright © 2011-2022 走看看