简体   繁体   中英

how do i split input values into 4 digit vector, like n bits into nibble per part vector?

I have a project to transmit soundwave to communicate with pcs.

The first step is that the input is a string of binaries with variable length (length could be 128 bit/digits or 256 bits/digits, as long as the number of digits is divisible with 4), and that length is inserted into a vector and every vector contains a nibble or 4 bit, so I can convert it into Hexadecimal and then use it to determine which DTMF tone it should transmit.

I wrote the code in a class where the function of digit splitting vector is called std::vector splitbit();

The reason I called short int because my application don't want to take too much space.

I tried with bitset but it only works with a constant number. If i used vector, it may work but the code will be too long and waste lots of resources.

The input is written in unsigned long long int, but the integer can only take 20 digits (2^64) at a time

This is the header code:

#pragma once
#include "stdafx.h"
class dtmf
{
private:
    unsigned int decimal,remainder,power;
    unsigned long long int bit;
    std::vector<std::vector<unsigned short>> dtmffreq;
    std::vector<unsigned short>selectfreq[16], index, bitsplit;
//  std::vector<unsigned unsigned long long int> bitlen[128];
public:
    dtmf();
    void inifreq();
    void printfreq();
    void bitinput();
    std::vector<unsigned short> getfreq();
    std::vector<unsigned short int> splitbit();
    unsigned int bit2dec();
};

This is the input function:

void dtmf::bitinput()
{
    std::cin >> bit;
}

std::vector<unsigned short int> dtmf::splitbit()
{
    for (size_t i = 0; i < 4; i++)
    {
        bitsplit.push_back((bit/=10)%10);
    }
    return bitsplit;
}

When I debug the system, the output of bitsplit only gives one bit at a time, like if I write 1101, the output is {1,1,0,1}, and if I write 11000110 I get {1,1,0,0,0,1,1,0} (if I change i to 8) and not {1100,0110}.

because you don't clear bitsplit in dtmf::splitbit() function. make it like this:

std::vector<unsigned short int> dtmf::splitbit()
{
    std::vector<unsigned short int> output;
    for (size_t i = 0; i < 4; i++)
    {
        output.push_back((bit/=10)%10);
    }
    return output;
}

Or if you want to stay with bitsplit :

std::vector<unsigned short int> dtmf::splitbit()
{
    bitsplit.clear(); //<- this is new
    for (size_t i = 0; i < 4; i++)
    {
        bitsplit.push_back((bit/=10)%10);
    }
    return bitsplit;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM