01-15-2015 11:59 AM
How can I convert a char[] array (acquired from user-input textbox) to IP address? There seems to be no previous discussion on it.
Solved! Go to Solution.
01-15-2015 12:35 PM
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 ()
01-15-2015 12:37 PM
hey,
I need to verify if the string is valid as an IP address.
01-15-2015 12:41 PM - edited 01-15-2015 12:42 PM
hey,
then you might want to change your current control to an IP control using the instrument driver found in toolslib\custctrl\IPctrl.fp
01-15-2015 04:01 PM
thanks, that solved it.
01-21-2015 08:45 AM - edited 01-21-2015 08:45 AM
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.
01-21-2015 09:04 AM - edited 01-21-2015 09:05 AM
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.
01-29-2015 12:50 PM
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?
01-30-2015 01:52 AM - edited 01-30-2015 01:53 AM
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
02-02-2015 08:09 AM - edited 02-02-2015 08:10 AM
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.