Solved

Masking a String (UNIX C)

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

707 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

13 Experts available now in Live!

Get 1:1 Help Now