Pages

Showing posts with label word. Show all posts
Showing posts with label word. Show all posts

September 8, 2013

NSInteger to Word number conversion

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

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