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;
}