Solved

Masking a String (UNIX C)

Posted on 1997-02-25
4
256 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now