Anyway the procedure works but I cannot perform the final operation.
private static void ConvIPtoLong (string strIP)
{
string [] lnIpAry = new string[4];
int indexOfq = 0, term1 =0;
int res = 0, intVal1 = 0, indw = 0, powPoint = 1;
UInt32 addOn = 2147483648;
string myString = "";
Regex r = new Regex ("(.)");
myString = strIP;
// strIP is an IP address quad of type: "192.168.0.1"
// expected output: 3232235521
lnIpAry = r.Split (strIP, 4, 1);
// honestly I do not understand how this thing works:
// Regex.split (string, int, int) - I checked the array after this
// function statement and found a total mess: pieces of my
// original strings, sometimes just one letter, or a tail of the
// input string still with the delimiters: '.'
// At the same time when I remove these statements
// (Regex x = new Regex (".") and lnIpAry = r.split (strIP,4,1) an exception happens during runtime.
Console.WriteLine ("The input is {0}", strIP);
for (int i = 1; i <= 4; i++)
{
indexOfq = myString.IndexOf (".");
if (indexOfq != -1)
{
lnIpAry
= myString.Substring (0,indexOfq);
myString = myString.Substring (indexOfq+1);
}
else
{
lnIpAry
= myString.Substring (0);
}
}
for (int i = 1; i <= 4; i++)
{
intVal1 = Convert.ToInt32( lnIpAry
);
powPoint = 1;
for ( int ii = 7-i; ii >= 4; ii-- )
{
powPoint *= 256;
}
term1 = intVal1 * powPoint;
res += term1;
}
// res += addOn;
// Everything works upto the last statement which I
// commented out. If I leave it in the code does not compile.
// get an error that the system cannot convert Uint type to Int.
// When I try to assign the number 2147483648
// to an int type I get another error message. If I convert all
// my Int types to Uint then I cannot use UInt value
// in string.substring (,) function--I get an error message in
// there.
// It is a hard job to keep C# type happy, I'll tell you.
Console.WriteLine ("Long IP address is {0}", res);
}
I had Console.write and WriteLine statements stuck at all parts of executable ladder which I removed here for clarity. They seemed to show that the code executes correctly and there were no arithmetic errors up to the last point where the shift must take place in the form of an addition of the largest Int number 2147483648. It does not work.
Incidentally, I got this whole idea from some code samples and they all talk about 2147483648 being the largest Long. In fact it is the largest Int, not Long. Why do they do it--could anyone explain it to me please.
Also I had hard time taking power of an integer. Are there any functions out there that can do it The Math.pow function refuses to deal with Int numbers. It is grievous. So much hardship in C# around simple matters. A very rigid approach.
Hope to get a comprehensive explanation, as always.
There are actually a few questions up there.
Many thanks in advance.

Hard to be a newbie: Type conversion problem.
Noodles_is_a_valid_display_name
DD77
I think you want something along the lines below. The command line argument is an ip address in string format - it prints out that ip address converted to its hexadecimal and integral equivalents.
using System;
class IPShift
{
public static void Main(string[] args)
{
if ( args.Length != 1 )
{
Console.WriteLine("usage: IPShift xxx.xxx.xxx.xxx");
Environment.Exit(-1);
}
const int BITS_PER_OCTET = 8;
uint int_ip = 0;
string str_ip = args[0];
string[] split_str_ip = str_ip.Split(".".ToCharArray());
for (int oct_idx=0; oct_idx < split_str_ip.Length; oct_idx++)
{
uint cur_octet = UInt32.Parse(split_str_ip[oct_idx]);
uint cur_octet_shifted = cur_octet << (oct_idx*BITS_PER_OCTET);
int_ip = int_ip | cur_octet_shifted;
}
Console.WriteLine("int_ip = 0x{0:x} (hex), = {0:d} (decimal)", int_ip);
}
}
Forgive the block local uint declarations but I thought they'd improve clarity.
trasherhits
gengisangelo
IPAddress address = IPAddress.Parse("127.0.0.1");
To split a string, you don't need to create a regular expression - use strIP.Split('.') instead. If you wanted to use a Regex, you would need to have a pattern of "\.", because "." is a placeholder for any character.
To take the power of an integer, use a bit shift: 1 << 8 == 256.
Dwayne A. Davis