Pages

Showing posts with label nsinteger. Show all posts
Showing posts with label nsinteger. 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

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

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

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

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

July 19, 2013

Greater Common Divisor (GCD) between 2 numbers

This time we're considering an easy math problem: the Greater Common Divisor between two integers.


/** Compute the Greater Common Divisor (GCD) for two numbers.

 @param num1 First number.
 @param num2 Second number.
 @return Returns the Greater Common Divisor (GCD).

 */
+ (NSInteger)greaterCommonDivisorForNumber1:(NSInteger)num1 andNumber2:(NSInteger)num2 {
    if (num1 < 0 || num2 < 0) {
        LogError(@"Numbers are negative. Cannot compute GCD");
    } else {
        NSInteger temp = num1 % num2;
        while (temp > 0) {
            num1 = num2;
            num2 = temp;
            temp = num1 % num2;
        }
    }
    return num2;
}



Download the ready-for-use source file (.m) of Greater Common Divisor (GCD) between 2 numbers

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)

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)

June 20, 2013

Int to binary

This convenient method converts an integer to a binary number.


/** Convert an int to a binary.

 @param val Integer value.
 @return Returns the binary value as string.

 */
+ (NSString *)binaryFromInt:(NSInteger)val {
    NSMutableString *str = [NSMutableString string];
    for (NSInteger num = val; num > 0; num >>= 1) {
        [str insertString:((num & 1) ? @"1" : @"0") atIndex:0]; // Prepend 0 or 1, depending on the bit.
    }
    return str;
}



Download the ready-for-use source file (.m) of Int to binary