Techie Talks

Thursday, March 09, 2006

Translate BitMask to NetMask for class C networks

Given a network address in the form of IPAddress/Bitmask, how would one get the netmask (or subnetmask) and the number of IP addresses available? This question cropped up when I was told that our company has been assigned a /29 ip address by NTS.

Note: In a 32-bit IP address, the number of bits used to identify the network and the host vary according to the network class of the address. In a Class C network, the first 3 bits, or the high-order bits, are always "110." The next 21 bits are used to define the Class C network, and the final eight bits are used to identify the host. The IP address is represented in dotted decimal notation of four 8-bit fields, or octets, that have been converted from binary to decimal numbers. (Source:

Answer: Every IP address of the form XYZ.XYZ.XYZ.XYZ (where 0 <= XYZ <= 255) can be represented by 32bits (4 bytes, 1 byte per group). If the bitmask of the class C network is /BM and the starting IP is ABC.DEF.GHI.JKL, then, Number of computers in the subnet (NC)= 2^(32-BM). IP Range = ABC.DEF.GHI.JKL to ABC.DEF.GHI.JKL + 2^NC - 1
Subnet mask = A binary string with all but the (32-BM) lower order bits as zero. (in the four octect form).

Example: A /29 subnet has
2^ (32 - 29) = 2 ^ 3 = 8 Ip address
IP adresses range: from to
Subnet mask: (1111 1111)2.(1111 1111)2.(1111 1111)2.(1111 1000)2 =

Wednesday, March 01, 2006

Read a webpage as a string

private string WebPageFromURL(string url)
WebRequest myWebRequest = WebRequest.Create(url);

// Send the 'WebRequest' and wait for response.
WebResponse myWebResponse = myWebRequest.GetResponse();

// Obtain a 'Stream' object associated with the response object.
Stream ReceiveStream = myWebResponse.GetResponseStream();

System.Text.Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

// Pipe the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(ReceiveStream, encode);
string readStr = readStream.ReadToEnd();

return readStr;