用零填充向量 [英] padding in vector with zeros

查看:121
本文介绍了用零填充向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的纯文本首先转换为二进制文本,然后应划分为64位块,并且密钥必须分别加密tese块.例如,如果我的文本有90位,则应在其后加上零,以使其具有128位.我不知道该怎么做.这是我的代码:

my plaintext is first converted to binary text, then it should be divided into 64 bit blocks and the key has to encrypted tese blocks separately. for example, if my text has 90 bits it should be supplemented with zeros so that it has 128 bits. I do not know how to do this. here is my code:

string ifile = "filetxt.txt";
string ofile = "file2.txt";
string key = "keywordd";
vector<int> k;
vector<int> txt;
char text;
vector<int> o;
int i;
int c = 0;
int d = 1;

void f() {
    ifstream ist ("filetxt.txt");
    ofstream ost ("file2.txt");
    int a[64], i;
    while (ist >> text) {
        for(char& text : key) {
            for(i=0; i < 8; i++){
                a[i] = text%2;
                text = text/2;
            }
            for(i=i-1; i >= 0 ;i--){
                k.push_back(a[i]);
            }
        }
        if (ist) {
            for(i=0; i < 8; i++){
                a[i] = text%2;
                text = text/2;
            }
            for(i=i-1 ;i >= 0 ;i--){
                txt.push_back(a[i]);
            }
            for(int j = 0; j < 8; j++) {
                if(k[j] == txt[j]) {
                    o.push_back(c);
                } else if (k[j] != txt[j]) {
                    o.push_back(d);
                }
            }
            for(i=0; i<8; i++) {
                ost << o[i];
            }
            for(i=0; i<8; i++) {
                cout << o[i];
            }
        }
    }
    cout << endl;
    for(i=0; i<64; i++) {
        cout << k[i];
    }
    cout << endl;
    for(i=0; i<64; i++) {
        cout << txt[i];
    }
}
int main()
{
    f();
    return 0;
}

我要做这样的事情:

if (txt.size()< 64){
    for(i= 0; i< 64- txt.size();i++){
        txt.push_back(c);
    }
}

我认为问题出在txt向量中,因为如果我要打印它,我

i think that the problem is in the txt vector because if i want to print it i

推荐答案

非常简单:

if (txt.size()< 64)
    txt.resize(64);

这将简单地将向量填充为默认值int,即0.

This will simply pad out the vector with the default value for int, which is 0.

如果您想要一个非0的值(如代码建议的c),则为:

If you want a value other than 0 (c as your code suggests) then it's:

if (txt.size()< 64)
    txt.resize(64, c);

这篇关于用零填充向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆