Code Samples
IP Parsing
This code parses the input IP address and extracts each octet for binary conversion.
- (void)convertToBinary:(NSMutableString *) input {
//the major chunk of this code extracts each octet of the IP address into it's own variable for processing.
NSString *intake = input;
NSRange decimalLocationOne;
NSRange decimalLocationTwo;
NSRange decimalLocationThree;
NSString *firstOctet;
NSString *secondOctet;
NSString *thirdOctet;
NSString *fourthOctet;
decimalLocationOne = [intake rangeOfString: @"."];
firstOctet = [intake substringToIndex: decimalLocationOne.location];
intake = [intake substringFromIndex: (decimalLocationOne.location + 1)];
[firstOctetLabel setText: firstOctet];
decimalLocationTwo = [intake rangeOfString: @"."];
secondOctet = [intake substringToIndex: decimalLocationTwo.location];
intake = [intake substringFromIndex: (decimalLocationTwo.location + 1)];
[secondOctetLabel setText: secondOctet];
decimalLocationThree = [intake rangeOfString: @"."];
thirdOctet = [intake substringToIndex: decimalLocationThree.location];
intake = [intake substringFromIndex: (decimalLocationThree.location + 1)];
[thirdOctetLabel setText: thirdOctet];
fourthOctet = intake;
[fourthOctetLabel setText: fourthOctet];
//once each octet is extracted into it's own variable, each is sent to the real binary converter,
//whose results are assigned to the appropriate variable
NSString *firstOctetBinary = [self binarySubConverter:(NSString *)firstOctet];
[firstOctetBinaryLabel setText: firstOctetBinary];
NSString *secondOctetBinary = [self binarySubConverter:(NSString *)secondOctet];
[secondOctetBinaryLabel setText: secondOctetBinary];
NSString *thirdOctetBinary = [self binarySubConverter:(NSString *)thirdOctet];
[thirdOctetBinaryLabel setText: thirdOctetBinary];
NSString *fourthOctetBinary = [self binarySubConverter:(NSString *)fourthOctet];
[fourthOctetBinaryLabel setText: fourthOctetBinary];
}
Binary Conversion
This code processes a given octet (as a string) and returns its binary representation. It is limited to processing numbers between 0 and 255, since other numbers were outside the scope of this project.
- (id)binarySubConverter:(NSString *)input {
NSString *intake = input;
int intakeInt = [intake intValue];
NSMutableString *finalBinaryString = [NSMutableString stringWithString: @""];
//fixes bug that won't display anything if number is zero
if (intakeInt == 0) {
[finalBinaryString appendString: @"00000000"];
return finalBinaryString;
} else {
while (intakeInt!= 0) {
//try to subtract 128, if the result won't be less than zero.
if (intakeInt - 128 >= 0) {
intakeInt = intakeInt - 128;
[finalBinaryString appendString: @"1"];
} else {
[finalBinaryString appendString: @"0"];
}
//try to subtract 64, if the result won't be less than zero
if (intakeInt - 64 >= 0) {
intakeInt = intakeInt - 64;
[finalBinaryString appendString: @"1"];
} else {
[finalBinaryString appendString: @"0"];
}
//try to subtract 32, if the result won't be less than zero
if (intakeInt - 32 >= 0) {
intakeInt = intakeInt - 32;
[finalBinaryString appendString: @"1"];
} else {
[finalBinaryString appendString: @"0"];
}
//getting how this works yet?
if (intakeInt - 16 >= 0) {
intakeInt = intakeInt - 16;
[finalBinaryString appendString: @"1"];
} else {
[finalBinaryString appendString: @"0"];
}
if (intakeInt - 8 >= 0) {
intakeInt = intakeInt - 8;
[finalBinaryString appendString: @"1"];
} else {
[finalBinaryString appendString: @"0"];
}
if (intakeInt - 4 >= 0) {
intakeInt = intakeInt - 4;
[finalBinaryString appendString: @"1"];
} else {
[finalBinaryString appendString: @"0"];
}
if (intakeInt - 2 >= 0) {
intakeInt = intakeInt - 2;
[finalBinaryString appendString: @"1"];
} else {
[finalBinaryString appendString: @"0"];
}
if (intakeInt - 1 >= 0) {
intakeInt = intakeInt - 1;
[finalBinaryString appendString: @"1"];
} else {
[finalBinaryString appendString: @"0"];
}
return finalBinaryString;
}
}
}