Solved

Masking a String (UNIX C)

Posted on 1997-02-25
4
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Suggested Solutions

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

752 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