读取12位输入c++

Reading 12 Bit input c++

本文关键字:c++ 输入 12位 读取      更新时间:2023-10-16

我正在尝试编写一个可以读写12位的小程序。输入应该没有任何问题,但我会将其包括在内,以便您更好地理解问题。输入应该由下面包含的OFStream12Bits.cpp/main.cpp创建为sample.lzw,输出应该从写入函数中读取sample.lzv。我在读取代码时遇到了输出问题和主要代码不匹配的问题。我认为问题来自运算符>>,而readBit函数并不完全确定。非常感谢你的帮助,我已经被困了一段时间了!

readbit的说明如下。。。

//basic readBit
//read12Bits(): 12Bit =
//declare Result : 12Bit = 0;
//for i = 1 to 12
//do
//declare lBit : Bit = get bit from input
//if(lBit == 1)
//then Result = (1 << (i-1)) + Result; //set bit at index i
//od
//return result

我不明白的是,我需要返回*this,但没有+运算符,所以我不能使用result来设置索引I处的位。目前我有这样的代码。

IFStream12Bits& IFStream12Bits::operator>>(int& a12BitValue)
{
    //int Result = a12BitValue;
    //a12BitValue = ((a12BitValue & 0x0fff) << 1);
    a12BitValue = a12BitValue & 0x0fff;

    for (int i = 0; i < 12; i++)
    {
        int bit = readBit();
        if (bit == 1)
        {
            a12BitValue = (1 << (i - 1)) + a12BitValue; //set bit at index i
        }
    }
    return *this;
}

此外,readBit的说明如下。。。

//implements mapping process. returns 0 or 1 depending on value of fBuffer[fByteIndex] & (1 << (fBitIndex - 1))
//see how it works with experiments
//at start check if (fByteCount == 0){reload();} then use reload() called as buffer does not contain any data before calling reload
//next fetch the bit store and then advance fByteIndex and fBitIndex 
//if fBitIndex(highest to lowest) reaches 0 you need to switch to the next byte in the buffer. and also decrment fByteCount
//then finally return result

代码是

int IFStream12Bits::readBit()
{
    if (fByteCount == 0){ reload(); }
    //int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));
    int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));
    int result = 0;
    cout << "bit: " << bit << endl;
    //added this just cause
    if (bit == 0)
    {
        result = 0;
    }
    else
    {
        result = 1;
    }
    //additional logic required?
    fByteIndex++;
    fBitIndex--;

    //switch to next byte in the buffer
    if (fBitIndex == 0)
    {
        fByteCount--;
        fBitIndex = 8;
        fByteIndex = 0;
    }
    return result;
}

如果您需要了解正在发生的事情,以下是完整的.cpp文件。。。IFStream12Bits.cpp

#include "IFStream12Bits.h"
#include <iostream>
using namespace std;

//default constructor
IFStream12Bits::IFStream12Bits()
{
    init();
}
//takes aFIleName
IFStream12Bits::IFStream12Bits(const char* aFileName)
{
    init();
    open(aFileName);
}
//deconstructor
IFStream12Bits::~IFStream12Bits()
{
    close();
}
//initialize the integer member variables with sensible values
//:fBuffer(), fByteCount(0), fByteIndex(0), fBitIndex(8)
//fBitIndex(highToLow)
void IFStream12Bits::init()
{
    for (int i = 0; i < 32; i++)
    {
        fBuffer[i] = 0;
    }
    fByteCount = 0;
    fByteIndex = 0;
    fBitIndex = 8;
}
//fills input buffer fBuffer with the next 32 bytes and sets fByteCount to number of bytes read
void IFStream12Bits::reload()
{
    //fills fBuffer with 32 bytes
    fIStream.read((char*)fBuffer, 32);
    //fIStream.read((char*)fBuffer, fByteIndex + (fBitIndex % 8 ? 1 : 0));
    //sets fByteCount to number of bytes read
    fByteCount = fIStream.gcount();
}
//implements mapping process. returns 0 or 1 depending on value of fBuffer[fByteIndex] & (1 << (fBitIndex - 1))
//see how it works with experiments
//at start check if (fByteCount == 0){reload();} then use reload() called as buffer does not contain any data before calling reload
//next fetch the bit store and then advance fByteIndex and fBitIndex 
//if fBitIndex(highest to lowest) reaches 0 you need to switch to the next byte in the buffer. and also decrment fByteCount
//then finally return result
int IFStream12Bits::readBit()
{
    if (fByteCount == 0){ reload(); }
    //int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));
    int bit = fBuffer[fByteIndex] & (1 << (fBitIndex - 1));
    int result = 0;
    cout << "bit: " << bit << endl;
    if (bit == 0)
    {
        result = 0;
    }
    else
    {
        result = 1;
    }
    //additional logic required?
    fByteIndex++;
    fBitIndex--;

    //switch to next byte in the buffer
    if (fBitIndex == 0)
    {
        fByteCount--;
        fBitIndex = 8;
        fByteIndex = 0;
    }
    return result;
}
void IFStream12Bits::open(const char* aFileName)
{
    fIStream.open(aFileName, std::fstream::binary);
}
void IFStream12Bits::close()
{
    fIStream.close();
}
bool IFStream12Bits::fail()
{
    return fIStream.fail();
}
//true if no bytes left in input stream (fByteCount == 0)(should be zero if never read anythign from fIStream)
bool IFStream12Bits::eof()
{
    return fByteCount == 0;
}
//read 12Bit codes from the bit input stream implements the read12Bits algorithm as shown in the tutorial
//basic readBit
//read12Bits(): 12Bit =
//declare Result : 12Bit = 0;
//for i = 1 to 12
//do
//declare lBit : Bit = get bit from input
//if(lBit == 1)
//then Result = (1 << (i-1)) + Result; //set bit at index i
//od
//return result
// multiply values by 2 to shift left???????????
IFStream12Bits& IFStream12Bits::operator>>(int& a12BitValue)
{
    //int Result = a12BitValue;
    //a12BitValue = ((a12BitValue & 0x0fff) << 1);
    a12BitValue = a12BitValue & 0x0fff;

    for (int i = 0; i < 12; i++)
    {
        int bit = readBit();
        if (bit == 1)
        {
            a12BitValue = (1 << (i - 1)) + a12BitValue; //set bit at index i
        }
    }
    return *this;
}

OFStream12Bits.cpp

#include "OFStream12Bits.h"
OFStream12Bits::OFStream12Bits()
{
    init();
}
OFStream12Bits::OFStream12Bits(const char* aFileName)
{
    init();
    open(aFileName);
}
OFStream12Bits::~OFStream12Bits()
{
    close();
}
void OFStream12Bits::init()
{
    for (int i = 0; i < 32; i++)
    {
        fBuffer[i] = 0;
    }
    fByteIndex = 0;
    fBitIndex = 8;
}
void OFStream12Bits::writeBit0()
{
    fBitIndex--;
    finishWriteBit();
}
void OFStream12Bits::writeBit1()
{
    fBuffer[fByteIndex] += 1 << (fBitIndex - 1);
    fBitIndex--;
    finishWriteBit();
}
void OFStream12Bits::finishWriteBit()
{
    if (fBitIndex == 0)
    {
        if (fByteIndex == 31)
        {
            fByteIndex++;
            //write full buffer to stream
            flush();
        }
        else
        {
            fByteIndex++;
            fBitIndex = 8;
        }
    }
}
void OFStream12Bits::open(const char* aFileName)
{
    fOStream.open(aFileName, std::ofstream::binary);
}
bool OFStream12Bits::fail()
{
    return fOStream.fail();
}
void OFStream12Bits::close()
{
    flush();
    fOStream.close();
}
void OFStream12Bits::flush()
{
    //                                     do we need to add last byte?
    fOStream.write((char*)fBuffer, fByteIndex + (fBitIndex % 8 ? 1 : 0));
    init();
}
OFStream12Bits& OFStream12Bits::operator<<(int a12BitValue)
{
    a12BitValue = a12BitValue & 0x0fff; // mask 12 lower bits
    for (int i = 0; i < 12; i++) //write 12 bits
    {
        if (a12BitValue & 0x01) // the current lowest bit is set
        {
            writeBit1();
        }
        else
        {
            writeBit0();
        }
        a12BitValue >>= 1; // code = code / 2 --shifting value accross
    }
    return *this;
}

main.cpp

#include "OFStream12Bits.h"
#include "IFStream12Bits.h"
#include <iostream>
using namespace std;
void write4096()
{
    cout << "Write 4096 codes" << endl;
    OFStream12Bits lWriter("sample.lzw");
    if (lWriter.fail())
    {
        cerr << "Error: unable to open output file" << endl;
        exit(1);
    }
    for (int i = 4096; i >= 0; i--)
    {
        lWriter << i;
    }
}
void read4096()
{
    cout << "Read 4096 codes" << endl;
    IFStream12Bits lInput("sample.lzw");
    if (lInput.fail())
    {
        cerr << "Error: unable to open input file!" << endl;
        exit(2);
    }
    for (int i = 4095; i >= 0; i--)
    {
        int l12BitValue; 
        lInput >> l12BitValue;
        if (l12BitValue != i)
        {
            cerr << "Error: Code mismatch: " << l12BitValue << " != " << i << endl;
            exit(3);
        }
    }
    if (!lInput.eof())
    {
        cerr << "Error: Input stream not exhausted" << endl;
    }
}
int main()
{
    write4096();
    read4096();
    cout << "SUCCESS" << endl;
    return 0;
}

您的输入代码从上一个值开始。您应该从0开始,因为您没有清除未设置的位。

IFStream12Bits& IFStream12Bits::operator>>(int& a12BitValue)
{
    a12BitValue = 0;
    for (int i = 0; i < 12; i++)
    {
        int bit = readBit();
        if (bit == 1)
        {
            a12BitValue = (1 << (i - 1)) + a12BitValue; //set bit at index i
        }
    }
    return *this;
}

此外,+在这里也可以工作,但在处理位时使用逐位操作更清楚。另外,我认为你的移位是关闭的。我会这样写设置位线:

a12BitValue |= 1 << i;

如果你想一想,当i为0时,你想设置第一个比特(即11 << 0。)当i为1时,你想要下一个比特,以此类推。所以你不需要减去一。

我不确定这是唯一的问题,但你可以尝试用单元测试独立测试每个类。例如,从一个原始字节缓冲区(如{0x89, 0xAB, 0xCD, 0xEF, 0x01})开始,然后读取三组12位。验证它们是否正确。然后创建一个空缓冲区,并向其中写入特定的位,并检查字节是否正确。

通过独立测试算法,并使用非常严格的输入/输出,您会发现确定缺陷要容易得多。