سؤال

I used 'strlen' to find the length of a string, call it string a. I then did some other things to create a binary string. The binary strings value is longer than string a. I want to return the binary string as long as string a. How would I do that? Let me try to code it out to maybe help clarify:

int main(int argc, char** argv)
{
    int i, j, k, l, prefix_length, sum;
    char *s, *dot, *binary_string, *ret_val, *temp_string;
    char buf[] = "10.29.246.49/32";
    s = strtok(buf, "/");
    prefix_length = strlen(s);
    for(i = 4; i > 0; i--){
            dot = strtok(s, ".");
            while (dot != NULL){
            j = atoi(dot);
        sum = sum + j;
        s = strtok(NULL, ".");
        }
        *binary_string = dec_to_bin(sum);
    }
    strcpy(temp_string, "0");
    for(l = prefix_length - strlen(binary_string); i > 0; i--){
        strcat(temp_string, binary_string);
        strcpy(binary_string, temp_string);
        strcpy(tempstring, "0");
    }
    ret_val = binary_string;
    return 0;
}

Also, can you look at my dec_to_bin and tell me if I'm calling it right and what have you:

char dec_to_bin(int decimal)
{
    char *ret;
    int d = decimal, i;
    for (i = 128; i >= 1; i = i/2){
        if(d / i){
            ret += '1';
            d -= i;
        }
        else
            ret += '0';
    }
    return *ret;
}
هل كانت مفيدة؟

المحلول

Your dec_to_bin is trying to convert a number to a string of '1's and '0's, but is only returning the first char value

You are defining ret as a char * pointer, but you are using it like a std::string which it is not. It is a pointer to memory, and you have to provide it with some memory to point to. As it is you are overwriting random memory, although in debug mode ret probably is initialised to 0, so you will just get a memory exception.

You could allocate the memory with malloc, but this will lead to a world of pain as the way you call the function will simply result in memory leaks.

If you have to use char* pointers and not std::string then I would suggest passing it a buffer to write the string to. You know the string will always be 8 characters long plus the null terminator

char buffer[9];
dec_to_bin(sum, buffer);

ret += '1' is not doing what you think it does. It is adding a char value to a char* pointer which is totally different. You need to store the character at the location pointed to by ret, and then move ret to point to the next location

*ret = '1';
ret = ret + 1;

or

*ret++ = '1';

When this finishes ret will point to the end of the string, so you can't return that. There is not much benefit from returning a value you passed to the routine, but if you must then you need to save it

char* dec_to_bin(int decimal, char *buffer)
{
    char *ret = buffer;
    int d = decimal, i;
    for (i = 128; i >= 1; i = i/2){
        if(d / i){
            *ret++ = '1';
            d -= i;
        }
        else
            *ret++ = '0';
    }
    return buffer;
}

You should run this program in a debugger, because that will teach you a lot about what is actually going on in your code

نصائح أخرى

If binary_string was a std::string, which it needs to be for binary_string += to work, then

std::string return_val = binary_string.substr(0, strlen(a));

If you are limited to char * then

int l = strlen(a);
char* return_val = new char[l + 1];
strncpy(return_val, binary_string, l);
return_val[l] = 0;

specify variable name in only first call to strtok(). for operations on same string again use strtok() as,

strtok(NULL,".");

to know about using strtok() read this link.
To get the binary string you want, you can follow this procedure,

  for(i = 4; i > 0; i--){
        dot = strtok(s, ".");
        if (dot != NULL){
                j = atoi(dot);
                sum=sum+j;
                s = strok(NULL, ".");
        }
        else{
                k = atoi(s);
                sum=sum+j;
        }
        //printf("%s\n", dot);
     }
     binarystring=dec2bin(sum);

you can reduce this loop and use,

        dot = strtok(s, ".");
        while (dot != NULL)
        {
                j = atoi(dot);
                sum=sum+j;
                s = strok(NULL, ".");
        }
        binarystring=dec2bin(sum);

here instead of adding binary number, you can add integers and then convert the sum to binary. the result will be same number right.dec2bin() should convert decimal to binary and return binary number as string. then you add code similar to this to make binary_string length same as length of a,

strcpy(tempstring,"0");
for(i=strlen(a)-strlen(binary_string);i>0;i--)
{
  strcat(tempstring,binary_string);
  strcpy(binary_string,tempstring);
  strcpy(tempstring,"0");
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top