Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Masking a String (UNIX C)

Posted on 1997-02-25
4
Medium Priority
?
272 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 90 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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

824 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