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
  • 相关阅读:
    Android 数据库框架 DBFlow 的使用
    Android进阶AIDL使用自定义类型
    Android进阶之AIDL的使用详解
    RecyclerView实现拖动排序和滑动删除功能
    RecyclerView的刷新分页
    RecyclerView 的 Item 的单击事件
    RecyclerView 的简单使用
    AutoCompleteTextView的简单使用
    Spinner的简单实用
    黎曼猜想
  • 原文地址:https://www.cnblogs.com/findumars/p/4735312.html
Copyright © 2011-2022 走看看