tags:

views:

115

answers:

6
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
+1  A: 

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++).

David Thornley
didn't work...same output.
b.j.g
This is true, but not the cause of the problem.
Porculus
+9  A: 

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.

Porculus
I included a \n in the printf statement... that seems to have solved the problem somewhat. I now get the correct value for Raw String Is 0001. However a 48 is printing out after that and I dont know why. Can you maybe go into a little more detail on why the printf statement would alter the value of an array?
b.j.g
To convert a textual digit to a corresponding integer, prefer subtracting `'0'` rather than 0x30. 0x30 is the ASCII code for '0'; but when switching to other text coding scheme, 0x30 may not work. Also `'0'` is more readable.
Thomas Matthews
A: 

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);
toby
This is not necessary. strcpy adds a null terminator.
Porculus
still getting the same output...
b.j.g
Procolus's answer is correct, I'll bet if you add that \n to the 1st printf you'll see 48 on a newline!
toby
A: 

strcpy does not pad the destination string with 0s. You allocate a 6 character buffer and modify only the first 4. The remaining 2 are simply what was in memory at initialization time.

You can memset the raw_string_char with 0 before calling strcpy or use one of the "safe" versions of strcpy, such as strncpy.

Edit: Also, you should be testing against '0' and '1'.

Andrei
This is wrong. strcpy DOES add a null terminator.
Porculus
Indeed it does. Don't know what i was thinking...
Andrei
+2  A: 

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?

UncleBens
A: 

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");
}
JohnPS