开发者

Extra numbers being appended to string, don't know why?

开发者 https://www.devze.com 2023-01-16 10:05 出处:网络
Number(string binary) { int raw_string_int[size]; char raw_string_char[size]; strcpy(raw_string_char,binary.c_str());
Number(string binary)
        {
            int raw_string_int[size];
            char raw_string_char[size];
            strcpy(raw_string_char,binary.c_str());
            printf("Raw String is %s",raw_string开发者_运维百科_char);

                for (int i=0;i<size;i++)
                {

                    raw_string_int[i] = int(raw_string_char[i]);
                    printf("%i\n",int(raw_string_char[i]));
                    if (raw_string_int[i] != 0 || raw_string_int[i] != 1)
                    {
                        printf("ERROR NOT A BINARY NUMBER\n");
                        exit(0);
                    }
                }

Hi, I'm entering 0001 as binary at the command prompt, but raw_string_char is being appended with two extra numbers. Can anyone explain to me why this is? Is the carriage return being brought in as a char?

Here is What I'm getting at the command prompt:

./test
0001
Raw String is 000148
ERROR NOT A BINARY NUMBER


You forgot the "\n" in your first printf. The 48 is from the second printf, and is the result of casting the first '0' (ASCII 0x30 = 48) to an int.

To convert a textual 0 or 1 to the corresponding integer, you need to subtract 0x30.


Your assumption that char('0') == int(0) and char('1') == int(1) just doesn't hold. In ASCII these characters have the values of 48 and 49.

What you should do to get integer values of digit characters is substract '0' instead of simple casting (raw_string_int[x] = raw_string_char[x] - '0';).

I think you have conceptual problems though. The array can't be full of valid values to the end (the corresponding C-string would at least contain a null-terminator, which is not a valid binary character). You can use the string's size() method to find out how many characters the string actually contains. And naturally you are risking buffer overflows, should the binary string contain size characters or more.

If the intention is to check if the input is a valid binary number, why can't you test the original string, why would you copy data around to two more arrays?


You're printing every character in raw_string_char. C-style strings go until the first zero character (that's '\0', not 0).

Change to for (int i = 0; raw_string_char[i] != 0 && i < size; i++).


Like others said, '0' is converted to an integer 48. You don't really need to convert the C++ string to a C style string. You can use iterators or the index operator [] on the C++ string. You also need to use logical AND && rather than logical OR || in your if statement.

#include<cstdio>
#include<string>

void Number(std::string binary) {
    for(std::string::const_iterator i = binary.begin(); i != binary.end(); i++ )
        if( *i != '0' && *i != '1')
        {
        printf("ERROR NOT A BINARY NUMBER\n");
            return;
        }
}

int main() {
    Number("0001");
}


The raw_string_char is never initialized, the extra characters are possibly due to this. Use memset to initialize the array.

memset(raw_string_array, 0, size);
0

精彩评论

暂无评论...
验证码 换一张
取 消