Solved

Masking a String (UNIX C)

Posted on 1997-02-25
4
261 Views
Last Modified: 2008-02-20
Given a string = "126.0.28.1" I want to use another string, mask = "255.0.0.0" and mask out the string above.
The result being "126.0.0.0".
I want this to work with:

"200.78.78.78" mask = "255.255.0.0"
To result in: "200.78.0.0"
Any Ideas or code that could help ?
0
Comment
Question by:r1ccard0
  • 2
4 Comments
 
LVL 1

Expert Comment

by:edtmihu
ID: 1249387
I don't hae any code, but an idea:
1. Split the strings into parts usinf strtok(...)
2. Convert the parts into integers
3. Mask the integers
4. Make a new string using sprintf(...)

Does this help you ?
Regards Michael
0
 
LVL 4

Expert Comment

by:emmons
ID: 1249388
Do you still want the code?
0
 

Author Comment

by:r1ccard0
ID: 1249389

0
 
LVL 4

Accepted Solution

by:
emmons earned 30 total points
ID: 1249390
int main()
{
char *String1 = "200.78.78.78";
char *Mask = "255.255.0.0";
char Result[128];
unsigned char Dectets[4], DectMask[4];

StrToNums( String1, Dectets);
StrToNums( Mask, DectMask);
sprintf( Result, "%d.%d.%d.%d",
        Dectets[0] & DectMask[0],
        Dectets[1] & DectMask[1],
        Dectets[2] & DectMask[2],
        Dectets[3] & DectMask[3]);
        printf( "The answer is %s\n", Result);
}



int StrToNums( char *String1, unsigned char *Num)
{
int i;
char *next_char;
char number;
unsigned char bin_val;
unsigned char additional;
next_char = String1;

        printf( "%s\n", String1);
        for( i=0; i<4; i++) {
                bin_val = 0;
                while(( number = *next_char++) != '.') {
                        printf( "nextchar = %c\n", number);
                        bin_val *= 10;
                        printf( "1 binval = %d (%o)\n", bin_val, bin_val);
                        additional = number - '0';
                        printf( "additional %d (%o)\n", additional, additional);
                        bin_val += additional;
                        printf( "2 binval = %d (%o)\n", bin_val, bin_val);
                        if( *next_char == '\0') break;
                }
                printf("Result = %d (%o)\n", bin_val, bin_val);
                Num[i] = bin_val;
        }
        printf( "%s = %d-%d-%d-%d\n", String1, Num[0], Num[1], Num[2], Num[3]);
        return(0);
}





0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question