-2

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;
}
Question_Guy
  • 323
  • 2
  • 3
  • 17
  • `char *s, *binary_string;` defines two pointers to `char`, but no "strings". – alk Mar 11 '14 at 08:11
  • can you give some input and output string to be clear and also tell what output you are getting. – LearningC Mar 11 '14 at 08:12
  • 1
    `strlen(s)` provokes undefined behaviuor as `s` hadn't been initialised. – alk Mar 11 '14 at 08:13
  • 1
    What do you expect this `binary_string += ...;` to do? – alk Mar 11 '14 at 08:14
  • The binary string is going through each section before the "." making it into binary, then adding it to what's already been converted to binary. – Question_Guy Mar 11 '14 at 08:21
  • I'm then to care about th first prefix_length number of binary digits...that's where the retrn_value comes in, which from what I understand is how I explained it but not sure how to go about it – Question_Guy Mar 11 '14 at 08:22
  • You are using binary_string like a std::string, but have declared it as a char *. You should decide which type you want to use, and get the code to compile – David Sykes Mar 11 '14 at 08:55
  • @DavidSykes So you're saying to change it to a string variable rather than a char. But then from there, I'd still run into the same problem. – Question_Guy Mar 11 '14 at 09:01
  • If it's a string you can use substr, otherwise you need to allocate memory and copy. My answer has the details – David Sykes Mar 11 '14 at 09:09
  • @Question_Guy can you roll back your question to original state. because of your modifications that you did from answers, answers have become not applicable. you should not change the question to reflect the changes suggested in answer. you can go through questions related to [this](http://meta.stackexchange.com/questions/134401/question-changed-after-two-months-and-answer-unaccepted?rq=1) on meta stackoverflow. – LearningC Mar 13 '14 at 09:43

3 Answers3

1

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;
David Sykes
  • 48,469
  • 17
  • 71
  • 80
  • My way in C will not work for the binary string operation I want to have it do? Also, with your char example, why are you setting `returnval[prefix_length]` to zero? – Question_Guy Mar 11 '14 at 09:08
  • 1
    Your way uses a char * that you do not allocate memory for. Adding two char *variables as in 'binary_string += dec2bin(j);' does not have a meaningful result. returnval[prefix_length]=0 is null terminating the strings, all C strings have to be null terminated – David Sykes Mar 11 '14 at 09:11
1

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

David Sykes
  • 48,469
  • 17
  • 71
  • 80
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");
}
Community
  • 1
  • 1
LearningC
  • 3,182
  • 1
  • 12
  • 19
  • Elaborate please. I don't quite understand. Give me a better example. – Question_Guy Mar 11 '14 at 08:25
  • And explain why because it doesn't make sense to me to put it as NULL? – Question_Guy Mar 11 '14 at 08:26
  • @Question_Guy its the standard of using strtok(). strtok() uses the previously passed string if you specify NULL as parameter. read this [link](http://stackoverflow.com/questions/3889992/how-does-strtok-split-the-string-into-tokens-in-c) – LearningC Mar 11 '14 at 08:30
  • So would I go `(NULL + 1, ".")`? - I'm wanting to start from the character after finding "." – Question_Guy Mar 11 '14 at 08:37
  • 1
    @Question_Guy strtok() creates tokens separated by delimeter. you just use it in its standard way. strtok(NULL,"."). it will start reading for next token after "." itself. – LearningC Mar 11 '14 at 08:39
  • @LearningC OK, so I changed it. Good info. Thank you. However, what about my original question? :) – Question_Guy Mar 11 '14 at 08:43
  • 1
    @Question_Guy means you want to convert these integers to binary and give the resulting binary number as string? for exmanple as "1111000011110000" of length same as a right? – LearningC Mar 11 '14 at 08:48
  • 1
    @Question_Guy ok. i added procedure to get tsuch binary string. – LearningC Mar 11 '14 at 09:08
  • Your strcat is overwriting statically stored memory. http://stackoverflow.com/questions/2589949/c-string-literals-where-do-they-go – David Sykes Mar 11 '14 at 09:38
  • @DavidSykes ok thanks. i made changes. it will work fine now. – LearningC Mar 11 '14 at 09:54
  • @LearningC If it's not too much to ask can you look at my above and updated code and tell me if the changes I made make sense and the dec_2_bin make sense? When I run it, it hangs – Question_Guy Mar 11 '14 at 20:20
  • @DavidSykes You as well – Question_Guy Mar 11 '14 at 20:38
  • @Question_Guy initialize sum=0, and set i = prefix_length - strlen not l = prefix_length - strlen. and dec_to_bin() function check the logic and `char * ret` is a pointer so allocate memory. better way is to allocate memory in main() and pass pointer to it to dec_to_bin(). and i dint get where you'll use retvalue. – LearningC Mar 12 '14 at 06:16