Pages

Showing posts with label hex. Show all posts
Showing posts with label hex. Show all posts

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

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

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)