Skip to main content
1 of 7
Frank
  • 335
  • 3
  • 11

Rot47 an NSString Category

As a JAVA native, i am always a litle worried when doing objective-c... memory leaks and pointers flying at my head...

All comments are welcome on the Category below, the Category functions are correct.

#import "NSString+r47.h"

@implementation NSString (r47)
-(NSString *) r47String
{
    const char *_string = [self cStringUsingEncoding:NSASCIIStringEncoding];
    int stringLength = [self length];
    char newString[stringLength+1];
    int x;
    for( x=0; x<stringLength; x++ )
    {
        unsigned int aCharacter = _string[x];
        if( 0x20 < aCharacter && aCharacter < 0x7F ) // from ! to ~
            newString[x] = (((aCharacter - 0x21) + 0x2F) % 0x5E) + 0x21;
        else  // Not an r47 character
            newString[x] = aCharacter;
    }
    newString[x] = '\0';
    NSString *rotString = [NSString stringWithCString:newString encoding:NSASCIIStringEncoding];
    DLog(@"%@ = %@",self,rotString);
    return rotString ;
}

@end

Note, you can also find the code at github

Frank
  • 335
  • 3
  • 11