Easy conversion between NSInteger and Word.
You need:
- http://objective-c-functions.blogspot.com/2013/08/int-to-hex-number-conversion.html
- http://objective-c-functions.blogspot.com/2013/08/hex-to-int-number-conversion.html
/** Convert an int to a Word defined by a low byte and a high byte.
@param intVal Integer value.
@return Returns an array in which first item is the low byte and second item is the high byte.
@see intFromWordWithLowByte:highByte:
*/
+ (NSArray *)highLowBytesFromInteger:(NSInteger)intVal {
NSString *hexVal = [[LMFunctions hexFromInt:intVal] substringFromIndex:2]; // Remove "0x".
NSMutableString *prefix = [[NSMutableString alloc] initWithString:@""];
NSInteger length = hexVal.length;
if (length < 4) {
for (NSInteger i=0; i<4-length; i++) {
[prefix appendString:@"0"];
}
}
hexVal = [NSString stringWithFormat:@"%@%@", prefix, hexVal];
NSString *hexValHigh = [hexVal substringToIndex:2];
NSString *hexValLow = [hexVal substringFromIndex:(hexVal.length - 2)];
Byte bH = [LMFunctions intFromHex:hexValHigh];
Byte bL = [LMFunctions intFromHex:hexValLow];
return [NSArray arrayWithObjects:[NSNumber numberWithInteger:bH], [NSNumber numberWithInteger:bL], nil];
}
Download the ready-for-use source file (.m) of NSInteger to Word number conversion
A useful and helpful collection library of common Objective-C functions, methods, classes and categories.
Showing posts with label int. Show all posts
Showing posts with label int. Show all posts
September 8, 2013
September 7, 2013
Word to NSInteger number conversion
Intermediate function that converts a Word into a NSInteger.
You need:
- http://objective-c-functions.blogspot.com/2013/08/hex-to-int-number-conversion.html
/** Convert a Word to an int.
@param lowByte The low byte of the word.
@param highByte The high byte of the word.
@return Returns the integer value of the word.
@see highLowBytesFromInteger:
*/
+ (NSInteger)intFromWordWithLowByte:(Byte)lowByte highByte:(Byte)highByte {
NSString *startHexAddressHigh = [NSString stringWithFormat:@"0x%X", highByte];
NSString *startHexAddressLow = [NSString stringWithFormat:@"0x%X", lowByte];
NSInteger res = [LMFunctions intFromHex:startHexAddressHigh] * 16 * 16;
res += [LMFunctions intFromHex:startHexAddressLow];
return res;
}
Download the ready-for-use source file (.m) of Word to NSInteger number conversion
You need:
- http://objective-c-functions.blogspot.com/2013/08/hex-to-int-number-conversion.html
/** Convert a Word to an int.
@param lowByte The low byte of the word.
@param highByte The high byte of the word.
@return Returns the integer value of the word.
@see highLowBytesFromInteger:
*/
+ (NSInteger)intFromWordWithLowByte:(Byte)lowByte highByte:(Byte)highByte {
NSString *startHexAddressHigh = [NSString stringWithFormat:@"0x%X", highByte];
NSString *startHexAddressLow = [NSString stringWithFormat:@"0x%X", lowByte];
NSInteger res = [LMFunctions intFromHex:startHexAddressHigh] * 16 * 16;
res += [LMFunctions intFromHex:startHexAddressLow];
return res;
}
Download the ready-for-use source file (.m) of Word to NSInteger number conversion
August 28, 2013
Check if a number is a valid Hex
May this NSString be a valid hexadecimal value?
With this useful Objective-C method you could easily know.
/** Check whether a number can be a valid hexadecimal.
@param num Hexadecimal number.
@return Returns true if given number is valid hexadecimal, false otherwise.
@see hexFromInt:
@see intFromHex:
*/
+ (BOOL)isHexadecimalNumber:(NSString *)num {
if ([num isEqualToString:@"0"] || [num isEqualToString:@"1"] || [num isEqualToString:@"2"] || [num isEqualToString:@"3"] || [num isEqualToString:@"4"] ||
[num isEqualToString:@"5"] || [num isEqualToString:@"6"] || [num isEqualToString:@"7"] || [num isEqualToString:@"8"] || [num isEqualToString:@"9"] ||
[[num uppercaseString] isEqualToString:@"A"] || [[num uppercaseString] isEqualToString:@"B"] || [[num uppercaseString] isEqualToString:@"C"] ||
[[num uppercaseString] isEqualToString:@"D"] || [[num uppercaseString] isEqualToString:@"E"] || [[num uppercaseString] isEqualToString:@"F"] )
{
return YES;
} else {
return NO;
}
}
Download the ready-for-use source file (.m) of Check if a number is a valid Hex
With this useful Objective-C method you could easily know.
/** Check whether a number can be a valid hexadecimal.
@param num Hexadecimal number.
@return Returns true if given number is valid hexadecimal, false otherwise.
@see hexFromInt:
@see intFromHex:
*/
+ (BOOL)isHexadecimalNumber:(NSString *)num {
if ([num isEqualToString:@"0"] || [num isEqualToString:@"1"] || [num isEqualToString:@"2"] || [num isEqualToString:@"3"] || [num isEqualToString:@"4"] ||
[num isEqualToString:@"5"] || [num isEqualToString:@"6"] || [num isEqualToString:@"7"] || [num isEqualToString:@"8"] || [num isEqualToString:@"9"] ||
[[num uppercaseString] isEqualToString:@"A"] || [[num uppercaseString] isEqualToString:@"B"] || [[num uppercaseString] isEqualToString:@"C"] ||
[[num uppercaseString] isEqualToString:@"D"] || [[num uppercaseString] isEqualToString:@"E"] || [[num uppercaseString] isEqualToString:@"F"] )
{
return YES;
} else {
return NO;
}
}
Download the ready-for-use source file (.m) of Check if a number is a valid Hex
August 24, 2013
Add zeros to a digit number
So you have a number and want to add zeros to it? Check out my convenient Objective-C method.
You need:
- http://objective-c-functions.blogspot.com/2013/08/nsinteger-to-nsstring.html
/** Returns a two digits number.
Prepends "0" if the number is between 0 and 9.
@param n Integer to manage.
@return Returns a string representing the two-digits number.
@see threeDigits:
*/
+ (NSString *)twoDigits:(NSInteger)n {
NSString *ret = [LMFunctions stringFromInt:n];
if (n>=0 && n<10) {
ret = [NSString stringWithFormat:@"0%d", n];
}
return ret;
}
Download the ready-for-use source file (.m) of Add zeros to a digit number
You need:
- http://objective-c-functions.blogspot.com/2013/08/nsinteger-to-nsstring.html
/** Returns a two digits number.
Prepends "0" if the number is between 0 and 9.
@param n Integer to manage.
@return Returns a string representing the two-digits number.
@see threeDigits:
*/
+ (NSString *)twoDigits:(NSInteger)n {
NSString *ret = [LMFunctions stringFromInt:n];
if (n>=0 && n<10) {
ret = [NSString stringWithFormat:@"0%d", n];
}
return ret;
}
Download the ready-for-use source file (.m) of Add zeros to a digit number
August 18, 2013
Hex to Int number conversion
Hexadecimal-to-NSInteger made easy with this helpful function.
/** Convert a hex to an int.
@param val Hexadecimal value as string.
@return Returns the integer value.
@see isHexadecimalNumber:
@see hexFromInt:
*/
+ (NSInteger)intFromHex:(NSString *)val {
NSUInteger res = 0;
NSScanner *scanner = [NSScanner scannerWithString:val];
[scanner scanHexInt:&res];
return (NSInteger)res;
}
Download the ready-for-use source file (.m) of Hex to Int number conversion
/** Convert a hex to an int.
@param val Hexadecimal value as string.
@return Returns the integer value.
@see isHexadecimalNumber:
@see hexFromInt:
*/
+ (NSInteger)intFromHex:(NSString *)val {
NSUInteger res = 0;
NSScanner *scanner = [NSScanner scannerWithString:val];
[scanner scanHexInt:&res];
return (NSInteger)res;
}
Download the ready-for-use source file (.m) of Hex to Int number conversion
August 17, 2013
Int to Hex number conversion
A simple but helpful function for int-to-hex conversion.
/** Convert an int to a hex.
@param val Integer value.
@return Returns the hexadecimal value as string.
@see isHexadecimalNumber:
@see intFromHex:
*/
+ (NSString *)hexFromInt:(NSInteger)val {
return [NSString stringWithFormat:@"0x%X", val];
}
Download the ready-for-use source file (.m) of Int to Hex number conversion
/** Convert an int to a hex.
@param val Integer value.
@return Returns the hexadecimal value as string.
@see isHexadecimalNumber:
@see intFromHex:
*/
+ (NSString *)hexFromInt:(NSInteger)val {
return [NSString stringWithFormat:@"0x%X", val];
}
Download the ready-for-use source file (.m) of Int to Hex number conversion
August 7, 2013
NSInteger to NSString
A convenient shortcut of [NSString stringWithFormat:] for NSInteger values.
/** Convert an int to its string representation.
@param n Integer to convert.
@return Returns the string value of the number.
@see stringFromInt:
@see stringFromFloat:usingSeparatorForDecimal:
@see stringFromBool:ofType:
*/
+ (NSString *)stringFromInt:(NSInteger)n {
return [NSString stringWithFormat:@"%i",n];
}
Download the ready-for-use source file (.m) of NSInteger to NSString
/** Convert an int to its string representation.
@param n Integer to convert.
@return Returns the string value of the number.
@see stringFromInt:
@see stringFromFloat:usingSeparatorForDecimal:
@see stringFromBool:ofType:
*/
+ (NSString *)stringFromInt:(NSInteger)n {
return [NSString stringWithFormat:@"%i",n];
}
Download the ready-for-use source file (.m) of NSInteger to NSString
July 12, 2013
Hex to int conversion (full string)
A pure C function that will do the hex-2-int conversion.
This function takes the whole hexadecimal string.
You need:
- http://objective-c-functions.blogspot.com/2013/07/hex-to-int-conversion-single-char.html
/** Convert a hex number to an int.
@param p Characters of the hexadecimal value.
@return Returns the integer value.
*/
int hex2(char *p) {
int i;
unsigned char c;
i = hex(*p++);
if (i < 0) return i;
c = (i << 4);
i = hex(*p);
if (i < 0) return i;
return c | i;
}
Download the ready-for-use source file (.m) of Hex to int conversion (full string)
This function takes the whole hexadecimal string.
You need:
- http://objective-c-functions.blogspot.com/2013/07/hex-to-int-conversion-single-char.html
/** Convert a hex number to an int.
@param p Characters of the hexadecimal value.
@return Returns the integer value.
*/
int hex2(char *p) {
int i;
unsigned char c;
i = hex(*p++);
if (i < 0) return i;
c = (i << 4);
i = hex(*p);
if (i < 0) return i;
return c | i;
}
Download the ready-for-use source file (.m) of Hex to int conversion (full string)
Hex to int conversion (single char)
A pure C function that will do the hex-2-int conversion.
This function takes only a single char.
/** Convert a hex char to an int.
@param c Single character representing a hexadecimal char.
@return Returns the integer value.
*/
int hex(char c) {
if ('0' <= c && c <= '9') return c - '0';
if ('a' <= c && c <= 'f') return c - 'a' + 10;
if ('A' <= c && c <= 'F') return c - 'A' + 10;
return -1;
}
Download the ready-for-use source file (.m) of Hex to int conversion (single char)
This function takes only a single char.
/** Convert a hex char to an int.
@param c Single character representing a hexadecimal char.
@return Returns the integer value.
*/
int hex(char c) {
if ('0' <= c && c <= '9') return c - '0';
if ('a' <= c && c <= 'f') return c - 'a' + 10;
if ('A' <= c && c <= 'F') return c - 'A' + 10;
return -1;
}
Download the ready-for-use source file (.m) of Hex to int conversion (single char)
Subscribe to:
Posts (Atom)