//
//  RNHSUtils.m
//
#import "RNHSUtils.h"

@implementation NSString (HSStringUtils)

- (NSData *) hs_dataFromStringDescription {
    NSCharacterSet *chars = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789ABCDEFabcdef"] invertedSet];
    BOOL isValid = (NSNotFound == [self rangeOfCharacterFromSet:chars].location);

    if(!isValid) {
        return nil;
    }

    NSMutableData *dataToSend = [[NSMutableData alloc] init];
    unsigned char whole_byte;
    char byte_chars[3] = { '\0','\0','\0' };
    int i;

    for(i=0; i < [self length]/2; i++)
    {
        byte_chars[0] = [self characterAtIndex:i*2];
        byte_chars[1] = [self characterAtIndex:i*2+1];
        whole_byte = strtol(byte_chars, NULL, 16);
        [dataToSend appendBytes:&whole_byte length:1];
    }
    return dataToSend;
}

@end

