索引字符串时传递的空终止符

时间:2018-06-20 04:53:14

标签: c++ null-terminated

我正在尝试从字符串数组中提取特定字符,并将其分配给新变量中的已定义索引。我的期望值是null终止符,因为我的字符串末尾似乎有随机的未定义字符,所以我遇到了问题。

我是C ++编程的新手,一般来说都是较低级别的编程。请注意,函数“ charBi”可以正常工作,但是在将“ charBi”的输出分配给“ strBi”函数中的变量“ binar”时,该函数不再起作用。我意识到该代码可能不太好,但是可以提供任何帮助,尤其是它与摆脱“ binar”字符串末尾的随机字符有关。

谢谢!

#include <iostream>
#include <array>

using namespace std;

//Program meant to output a string of binary for an input word or phrase

//library of letter and binary pairs
char letterNumber[27][10]={"A01000001","B01000010","C01000011","D01000100","E01000101","F01000110","G01000111",
    "H01001000","I01001001","J01001010","K01001011","L01001100","M01001101","N01001110",
    "O01001111","P01010000","Q01010001","R01010010","S01010011","T01010100","U01010101",
    "V01010110","W01010111","X01011000","Y01011001","Z01011010"," 01011111"};

//finds binary number associated with input character. One character input
string charBi(char inputVar){ //WHY DOES THIS ONLY WORK IF THE FUNCTION IS A STRING?
    //loop setup
    int n=0;
    int last=sizeof(letterNumber)/sizeof(letterNumber[0]); // equal 27
    //loops through each of the strings in letterNumber
    while (n!=last) {
        if (letterNumber[n][0]==inputVar){ // if the letter is equal to input letter
            char bina[8]; //number of numbers following a letter
            for(int i=1;i<9;i++){ // writes the number associated with the letter to bina
                bina[i-1]=letterNumber[n][i]; // assigns number to specific index
            }
            return bina; //BINA DEFINED AS CHAR, BUT OUTPUTTING AS STRING
        }
        n++;
    }
}

//forms binary string of numbers for input phrase
string strBi(string strg){ //WHY DOES THIS ONLY WORK IF THE FUNCTION IS A STRING?
    char binar[8*strg.length()]; //binary for each character is 8 numbers long
    for(int i=0;i<strg.length();i++){ // for every letter in the input phrase
        string chbi=charBi(strg[i]); //gets binary for individual letter from charBi function
        cout<<"charbi sends: "<<chbi<<endl; //for debugging
        for(int n=0;n<8;n++){ //for every 1 or 0 in the binary for an idividual letter
            binar[(8*i)+n]=chbi[n]; // assign in order to string binar
        }
            cout<<"binar updates to: "<<binar<<endl; //for debugging
            getchar(); //for debugging
    }
    return binar; //BINAR DEFINED AS CHAR, BUT OUTPUTTING AS STRING
}

int main(){
    cout<<"final string is: "<<strBi("HELLO WORLD");
    return 0;
}

2 个答案:

答案 0 :(得分:3)

由于未正确终止数组,因此该程序未定义。

为了存储k字母字符串,您需要使用k + 1元素数组并将其终止– char bina[9] = {};char binar[8*strg.length() + 1] = {};应该可以解决问题。

但是您可以通过将C放在后面来简化一些事情:

std::map<char, std::string> letterNumber =
{{'A', "01000001"},
 {'B', "01000010"},
 // ...
 {' ', "01011111}"}};

//forms binary string of numbers for input phrase
std::string strBi(const std::string& strg)
{
    std::string binar;
    binar.reserve(8 * strg.size());
    std::for_each(strg.begin(), strg.end(), [&binar](char c) { binar += letterNumber[c]; });
    return binar;
}

答案 1 :(得分:0)

binar延长一个字符(char binar [8 * strg.length() + 1];),并将最后一个字符设置为NUL(就在返回之前,执行binar[8 * strg.length()] = '\0';