Click here to Skip to main content
15,891,372 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
JokeRe: MQOTD Pin
Stefto10-Apr-15 2:02
professionalStefto10-Apr-15 2:02 
GeneralFriday Programming Challenge Pin
PIEBALDconsult9-Apr-15 19:28
mvePIEBALDconsult9-Apr-15 19:28 
JokeRe: Friday Programming Challenge Pin
V.9-Apr-15 20:14
professionalV.9-Apr-15 20:14 
GeneralRe: Friday Programming Challenge Pin
CDP18029-Apr-15 20:37
CDP18029-Apr-15 20:37 
GeneralRe: Friday Programming Challenge Pin
User 101325469-Apr-15 22:09
User 101325469-Apr-15 22:09 
GeneralRe: Friday Programming Challenge Pin
CDP18029-Apr-15 22:26
CDP18029-Apr-15 22:26 
JokeRe: Friday Programming Challenge Pin
Joan M9-Apr-15 20:38
professionalJoan M9-Apr-15 20:38 
GeneralRe: Friday Programming Challenge Pin
C3D19-Apr-15 21:44
professionalC3D19-Apr-15 21:44 
Just to make sure i'm understanding everything right.
You want to convert CIDR notation to dotted decimal?!

My approach is to convert the CIDR to a binary string (eg. /8 = 11111111000000000000000000000000), then split this string to the octets in an array (eg. [0]=11111111 [1]=00000000 [2]=00000000 [3]=00000000) and then converting each octet to decimal.

Sample source in C#:
C#
public enum IP_TYPE
{
	IPv4
,	IPv6
}

public String convertCIDR2DottedDecimal(int nCIDR, IP_TYPE eType)
{
	// Build Binary String
	int nBits = (eType == IP_TYPE.IPv4) ? 32 : 128;
	String sBinary = "";

	for(int nIdx = nCIDR - 1; nIdx >= 0; nIdx--)
		sBinary += "1";

	for(int nIdx = nBits - nCIDR; nIdx >= 0; nIdx--)
		sBinary += "0";

	// Split Binary String to Octets
	int nOctets = (eType == IP_TYPE.IPv4) ? 4 : 16;
	String[] aOctets = new String[nOctets];

	for(int nIdx = 0; nIdx < nOctets; nIdx++)
	{
		String sOctet = "";
		for(int nOctetIdx = 0; nOctetIdx < 8; nOctetIdx++)
		{
			sOctet += sBinary[nOctetIdx];
		}

		sBinary = sBinary.Substring(8);

		// Convert each Octet to Decimal
		aOctets[nIdx] = "" + Convert.ToInt32(aOctets[nIdx], 2);
	}

	return String.Join(".", aOctets);
}


modified 10-Apr-15 17:27pm.

GeneralRe: Friday Programming Challenge Pin
Thomas Daniels9-Apr-15 21:44
mentorThomas Daniels9-Apr-15 21:44 
GeneralRe: Friday Programming Challenge Pin
Mark_Wallace9-Apr-15 21:54
Mark_Wallace9-Apr-15 21:54 
GeneralRe: Friday Programming Challenge Pin
Orjan Westin9-Apr-15 22:47
professionalOrjan Westin9-Apr-15 22:47 
GeneralRe: Friday Programming Challenge Pin
PIEBALDconsult10-Apr-15 11:30
mvePIEBALDconsult10-Apr-15 11:30 
GeneralRe: Friday Programming Challenge Pin
Freak3010-Apr-15 2:47
Freak3010-Apr-15 2:47 
GeneralRe: Friday Programming Challenge Pin
PIEBALDconsult10-Apr-15 11:31
mvePIEBALDconsult10-Apr-15 11:31 
GeneralHappy Friday Guys Pin
VijayPd9-Apr-15 18:24
VijayPd9-Apr-15 18:24 
GeneralRe: Happy Friday Guys Pin
King Fisher9-Apr-15 18:37
professionalKing Fisher9-Apr-15 18:37 
GeneralRe: Happy Friday Guys Pin
VijayPd9-Apr-15 19:04
VijayPd9-Apr-15 19:04 
JokeRe: Happy Friday Guys Pin
Agent__0079-Apr-15 18:38
professionalAgent__0079-Apr-15 18:38 
GeneralRe: Happy Friday Guys Pin
drummerboy05119-Apr-15 19:32
professionaldrummerboy05119-Apr-15 19:32 
GeneralRe: Happy Friday Guys Pin
HobbyProggy9-Apr-15 20:28
professionalHobbyProggy9-Apr-15 20:28 
GeneralRe: Happy Friday Guys Pin
Agent__0079-Apr-15 20:42
professionalAgent__0079-Apr-15 20:42 
GeneralRe: Happy Friday Guys Pin
Sascha Lefèvre9-Apr-15 23:22
professionalSascha Lefèvre9-Apr-15 23:22 
GeneralRe: Happy Friday Guys Pin
Agent__0079-Apr-15 23:37
professionalAgent__0079-Apr-15 23:37 
GeneralRe: Happy Friday Guys Pin
Corporal Agarn10-Apr-15 2:39
professionalCorporal Agarn10-Apr-15 2:39 
GeneralRe: Happy Friday Guys Pin
Agent__00712-Apr-15 17:23
professionalAgent__00712-Apr-15 17:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.