简体   繁体   English

如何将输入值拆分为 4 位向量,例如将 n 位拆分为每个部分向量的半字节?

[英]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.第一步是输入是可变长度的二进制字符串(长度可以是 128 位/位或 256 位/位,只要位数可以被 4 整除),然后将该长度插入到向量中每个向量都包含一个半字节或 4 位,因此我可以将其转换为十六进制,然后使用它来确定它应该传输的 DTMF 音调。

I wrote the code in a class where the function of digit splitting vector is called std::vector splitbit();我在 class 中编写了代码,其中数字分割向量的 function 称为 std::vector splitbit();

The reason I called short int because my application don't want to take too much space.我之所以调用 short int 是因为我的应用程序不想占用太多空间。

I tried with bitset but it only works with a constant number.我尝试使用 bitset,但它只适用于一个常数。 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输入以 unsigned long long int 形式写入,但 integer 一次只能取 20 位 (2^64)

This is the header code:这是 header 代码:

#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:这是输入 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}.当我调试系统时,bitsplit 的 output 一次只给出一位,比如我写 1101,output 是 {1,1,0,1},如果我写 11000110 我得到 {1,10, ,0,0,1,1,0}(如果我将 i 更改为 8)而不是 {1100,0110}。

because you don't clear bitsplit in dtmf::splitbit() function.因为您没有清除dtmf::splitbit() bitsplit中的 bitsplit。 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 :或者,如果您想继续使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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