0

I am trying pull specific characters from a string array, and assign them to defined indices in a new variable. I am having issues with what I expect is the null terminator, as there appear to be random assortment of undefined characters at the end of my strings.

I am new to coding in C++, and lower level programming in general. Note that the function "charBi" works perfectly, but it no longer works when assigning the output of "charBi" to the variable "binar" in the "strBi" function. I realize the code is probably not great, but any help is welcome, especially as it relates to getting rid of the random characters at the end of my "binar" string.

Thanks!

#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;
}
  • The character name is NUL, with one L. As in NUL-terminator and NUL-terminated string. – yzt Jun 20 '18 at 05:03
  • Your `BINA` and `BINAR` are *not* defined as `char`s. They are *arrays* of characters. However, for really old and really technical reasons, you can't return simple arrays from a function. – yzt Jun 20 '18 at 05:17
  • Good write-up on what happens when you return an array in [What is array decaying?](https://stackoverflow.com/questions/1461432/what-is-array-decaying) – user4581301 Jun 20 '18 at 05:54

2 Answers2

3

Since you didn't properly terminate your arrays, the program is undefined.

In order to store a k-letter string, you need to use a k+1-element array and terminate it – char bina[9] = {}; and char binar[8*strg.length() + 1] = {}; should do the trick.

But you can simplify things a bit by leaving C behind:

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;
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • upvoted. just one thought, maybe you meant program is *ill-formed* since *undefined* would describe behavior (i.e. UB)? or grammar wise, they are just the same? :) – Joseph D. Jun 20 '18 at 06:34
0

Make binar one character longer (char binar [8 * strg.length() + 1];) and set the last character to NUL (just before returning, do binar[8 * strg.length()] = '\0';)

yzt
  • 8,873
  • 1
  • 35
  • 44