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
No comments:
Post a Comment