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
  • 相关阅读:
    Binary Search Tree Iterator 解答
    Invert Binary Tree 解答
    Min Stack 解答
    Trapping Raining Water 解答
    Candy 解答
    Jump Game II 解答
    Implement Hash Map Using Primitive Types
    Gas Station 解答
    Bucket Sort
    HashMap 专题
  • 原文地址:https://www.cnblogs.com/findumars/p/4735312.html
Copyright © 2011-2022 走看看