LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert char[] array to IP address

Solved!
Go to solution

How can I convert a char[] array (acquired from user-input textbox) to IP address?  There seems to be no previous discussion on it.

0 Kudos
Message 1 of 10
(7,621 Views)

The IP address is a character array, isn't it?

 

Why do you want to convert it? To what? To four numbers? Then you might consider sscanf ()

 

 

0 Kudos
Message 2 of 10
(7,615 Views)

hey,

I need to verify if the string is valid as an IP address.

0 Kudos
Message 3 of 10
(7,611 Views)

hey,

 

then you might want to change your current control to an IP control using the instrument driver found in toolslib\custctrl\IPctrl.fp Smiley Wink

Message 4 of 10
(7,609 Views)

thanks, that solved it.

0 Kudos
Message 5 of 10
(7,600 Views)

This is how I solved the problem: I set a textbox with the content acquired from a text file that I want to check.  Then I use this line:

int IPchk = IPCtrl_ConvertFromString (tab, MyTextbox);

 

Based on the value of IPchk, I can tell if that acquired content is a legible IP or not.

 

Question:
I have to use an intermediate step by dumping the content I want to check into some textbox.  Why is there not a function that works straight on a char array[]?  That seems quicker to me.

0 Kudos
Message 6 of 10
(7,539 Views)
Solution
Accepted by topic author hn_sofec

Question:
I have to use an intermediate step by dumping the content I want to check into some textbox.  Why is there not a function that works straight on a char array[]?  That seems quicker to me.


The IP control is intended as a UI control where the user can interactively input some text to be used as a IP address. The instrument confines user input to "legal" characters for an IP address (e.g. only numbers and periods are allowed).

Since the instrument is distributed with full code, you may want to examine IsIPvalid (char *string) function inside the code: the function is not exposed by the instrument, nevertheless you can check the code and ultimately transfer it to your application if you consider it valid for your purposes.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 7 of 10
(7,534 Views)

Roberto,

I thought that function helps.  but apparently, it accepts 257.257.257.257 as a valid IP.  any idea on how to fix it?

0 Kudos
Message 8 of 10
(7,455 Views)

Which CVI release are you testing it on? 'cause I tested it in CVI2012SP1 and it works correctly.

 

I tested with:

char   a[32];

PromptPopup ("IP checking", "Input IP to check", a, 32);
DebugPrintf ("IP %s is %s\n", a, IsIPvalid (a) ? "valid" : "not valid");

 

Here the result of some tests:

IP 123 is valid
IP 1230123.123.123 is not valid
IP 123.123.123.123 is valid
IP 127.0.0.1 is valid
IP 127.0.0.256 is not valid
IP 127.0.0.255 is valid
IP 257.0.0.0 is not valid
IP 1.257.0.0 is not valid
IP 1.1.257.0 is not valid
IP 1.1.1.257 is not valid

IP 257.257.257.257 is not valid

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 9 of 10
(7,438 Views)

I am using CVI 2013 SP2.  This is the content of the said function:

 

static int IsIPvalid(char *string)
{
    int     IP[4]={0,0,0,0},curr=0,digits=0;
    char    lastChar=0;

    if( string==NULL )
        return 0;

    if( *(string=SkipWhiteSpace(string))=='\0' )
        return 1;   // Empty string is valid

    while( *string ) {
        if( isdigit(*string) ) {
            if( digits==3 || (IP[curr]=10*IP[curr]+(*string-'0'))>255 )
                return 0;
            ++digits;
        } 

        else if( *string=='.' ) {
            if( ++curr>3 || lastChar==0 || lastChar=='.' )  // Don't allow more than 3 periods or period at beginning or two consequential periods
                return 0;
            digits = 0;
    } 

    else    // Other characters not allowed
            return 0;
        lastChar = *string++;
    }

    return 1;
}

 

I just check again this morning and it works fine.  However, it did not work last week.  Not a single clue how that happened.  I already write my own code that checks the IP validity.

0 Kudos
Message 10 of 10
(7,403 Views)