Hex String Detected as Base64 - ios

I'm passing Hex string to this method but it still detects the string as base64.
My string is: 546869732069732073696d706c6520737472696e672e
+(BOOL)isBase64Data:(NSString *)input
{
input=[[input componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString:#""];
if ([input length] % 4 == 0) {
static NSCharacterSet *invertedBase64CharacterSet = nil;
if (invertedBase64CharacterSet == nil) {
invertedBase64CharacterSet = [[NSCharacterSet
characterSetWithCharactersInString:
#"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="]
invertedSet];
}
BOOL isbase64 = [input rangeOfCharacterFromSet:invertedBase64CharacterSet
options:NSLiteralSearch].location == NSNotFound;
return isbase64;
}
return NO;
}
Have followed several links on net with above code marked true. But somehow it's not working with the string I provided.

Related

ABRecordCopyCompositeName and CFBridgingRelease crash issue

I am developing IOS application using AddressBook.
Here is my code what I used.
I am getting crash issue on substringWithRange function.
What is the crash reason?
Thank you.
NSString * sort_name = CFBridgingRelease(ABRecordCopyCompositeName(person));
if (sort_name != nil) {
[self Make_Sorting_Name:sort_name];
- (NSDictionary *)Make_Sorting_Name:(NSString *)sort_name {
NSString * sort_char = [[NSString stringWithString:[sort_name substringWithRange:NSMakeRange(0, 1)]] uppercaseString];
NSCharacterSet *nonDigits = [NSCharacterSet letterCharacterSet];
BOOL containsNonDigitChars = ([sort_char rangeOfCharacterFromSet:nonDigits].location == NSNotFound);
}
The ABRecordCopyCompositeName function might return nil or empty string sometimes. So the case needs to be checked:
NSString *sort_char = #""; //or another specific character for sorting
if (sort_name != nil && sort_name.length > 0){
sort_char = [[NSString stringWithString:[sort_name substringWithRange:NSMakeRange(0, 1)]] uppercaseString];
}

How to detect if the string is Base64 encoded or not? [Objective-c]

I am struggling here to make this work, i have to detect the string which i am receiving in the response is base64 encoded or not and handle it accordingly.
I have tried two ways so far:
1.
if ([input length] % 4 == 0 && input.length >= 3) {
static NSCharacterSet *invertedBase64CharacterSet = nil;
if (invertedBase64CharacterSet == nil) {
invertedBase64CharacterSet = [[NSCharacterSet characterSetWithCharactersInString:#"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="]invertedSet];
}
return [input rangeOfCharacterFromSet:invertedBase64CharacterSet options:NSLiteralSearch].location == NSNotFound;
}
return NO;
2.
NSString *expression = regexString;
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:&error];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, [self length])];
if (numberOfMatches > 0) {
return YES;
} else {
return NO;
}
Everything is working fine except when the string is 4 characters in length like 'Adam' both above snippets considers this as a base64 string which is not correct.
If anyone know better way to detect the Base64 string please let me know and it should work for string of 4 characters in length also.
Thanks in advance.

How to get the first alphabet character of a string in iOS

I have an example NSString in iOS
NSString* str = #"-- This is an example string";
I want to get the first alphabet letter. The result of above situation is letter "T" from word "This". Some characters before letter "T" is not alphabet letter so it returns the first alphabet letter is "T".
How can I retrieve it? If the string not contain any alphabet letter, it can return nil.
Besides, the result can be a NSRange
NSRange range = [string rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]];
First create a NSCharecterSet as a global variable and write this code
-(void)viewDidLoad{
NSCharacterSet *s = [NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"]
s = [s invertedSet];
NSString *myString = #"--- This is a string";
NSArray *arrayOfStrings = [myString componentsSeparatedByString:#" "];
for(int i=0;i<arrayOfStrings.count){
NSString *current = [arrayOfStrings objectAtIndex:i];
char c = [self returnCharacter:current];
if(c == nil){
//that means first word is not with alphabets;
}
else {
NSLog(#"%c",c);
//your output.
}
}
}
And here is the method
-(char)returnChracter:(NSString*)string{
NSRange r = [string rangeOfCharacterFromSet:s];
if (r.location != NSNotFound) {
NSLog(#"the string contains illegal characters");
return nil;
}
else {
//string contains all alphabets
char firstLetter = [string charAtIndex:0];
return firstLetter;
}
}
You can use the following function. Pass a string and get first character as a string.
-(NSString*)getFirstCharacter:(NSString*)string
{
for(int i=0;i<string.length;i++)
{
unichar firstChar = [string characterAtIndex:i];
NSCharacterSet *letters = [NSCharacterSet letterCharacterSet];
if ([letters characterIsMember:firstChar]) {
return [NSString:stringWithFormat:#"%c",firstChar];
}
}
return nil;
}

How can i check string contains unknown character?

I am calling webservice in the response i am getting the string.
When i print the string in NSLog it return empty string and When i check the length it returns 1.
So my problem is that how can i check the string is empty or not.
#define CHECK_NA_STRING(str) (str == (id)[NSNull null] || [str length] == 0)?#"N/A":str
NSLog(#"%#",CHECK_NA_STRING([dict objectForKey:#"ADDRESS_A"])); // nothing empty string
NSLog(#"%d",[CHECK_NA_STRING([dict objectForKey:#"ADDRESS_A"]) length]); // return 1
So how can i check that string is empty or not?
Thanks.
So the string is just a space? Then it will still have a length of 1.
Try:
NSString* string = ...;
if([string isKindOfClass:[NSString class]])
{
NSCharacterSet* invertedWhitespaceSet = [[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet];
const NSRange nonEmptyCharacterRange = [string rangeOfCharacterFromSet:invertedWhitespaceSet options:NSCaseInsensitiveSearch];
if(nonEmptyCharacterRange.location == NSNotFound)
{
// Empty invalid string
}
else
{
// Non-empty valid string
}
}
The string is not considered empty if it contains a binary zero (null character). For instance, try this code:
#define CHECK_NA_STRING(str) (str == (id)[NSNull null] || [str length] == 0)?#"N/A":str
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:#"\0" forKey:#"ADDRESS_A"];
NSLog(#"%#",CHECK_NA_STRING([dict objectForKey:#"ADDRESS_A"])); // nothing empty string
NSLog(#"%d",[CHECK_NA_STRING([dict objectForKey:#"ADDRESS_A"]) length]); // return 1
Nothing will print for the first NSLOG, but the second will print a "1". Indeed the string is one character long; it just messes up your NSLOG.
You probably want to test for some valid range of responses, or some invalid range. Perhaps, you could use a regular expression.

How to detect if NSString contains a particular character or not?

i have one NSString object , for ex:- ($45,0000)
Now i want to find if this string contains () or not
How can i do this?
Are you trying to find if it contains at least one of ( or )? You can use -rangeOfCharacterFromSet::
NSCharacterSet *cset = [NSCharacterSet characterSetWithCharactersInString:#"()"];
NSRange range = [mystr rangeOfCharacterFromSet:cset];
if (range.location == NSNotFound) {
// no ( or ) in the string
} else {
// ( or ) are present
}
The method below will return Yes if the given string contains the given char
-(BOOL)doesString:(NSString *)string containCharacter:(char)character
{
return [string rangeOfString:[NSString stringWithFormat:#"%c",character]].location != NSNotFound;
}
You can use it as follows:
NSString *s = #"abcdefg";
if ([self doesString:s containCharacter:'a'])
NSLog(#"'a' found");
else
NSLog(#"No 'a' found");
if ([self doesString:s containCharacter:'h'])
NSLog(#"'h' found");
else
NSLog(#"No 'h' found");
Output:
2013-01-11 11:15:03.830 CharFinder[17539:c07] 'a' found
2013-01-11 11:15:03.831 CharFinder[17539:c07] No 'h' found
- (bool) contains: (NSString*) substring {
NSRange range = [self rangeOfString:substring];
return range.location != NSNotFound;
}
I got a generalized answer to your question which I was using in my code. This code includes the following rules:
1. No special characters
2. At least one capital and one small English alphabet
3. At least one numeric digit
BOOL lowerCaseLetter,upperCaseLetter,digit,specialCharacter;
int asciiValue;
if([txtPassword.text length] >= 5)
{
for (int i = 0; i < [txtPassword.text length]; i++)
{
unichar c = [txtPassword.text characterAtIndex:i];
if(!lowerCaseLetter)
{
lowerCaseLetter = [[NSCharacterSet lowercaseLetterCharacterSet] characterIsMember:c];
}
if(!upperCaseLetter)
{
upperCaseLetter = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:c];
}
if(!digit)
{
digit = [[NSCharacterSet decimalDigitCharacterSet] characterIsMember:c];
}
asciiValue = [txtPassword.text characterAtIndex:i];
NSLog(#"ascii value---%d",asciiValue);
if((asciiValue >=33&&asciiValue < 47)||(asciiValue>=58 && asciiValue<=64)||(asciiValue>=91 && asciiValue<=96)||(asciiValue>=91 && asciiValue<=96))
{
specialCharacter=1;
}
else
{
specialCharacter=0;
}
}
if(specialCharacter==0 && digit && lowerCaseLetter && upperCaseLetter)
{
//do what u want
NSLog(#"Valid Password %d",specialCharacter);
}
else
{
NSLog(#"Invalid Password %d",specialCharacter);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Please Ensure that you have at least one lower case letter, one upper case letter, one digit and No Any special character"
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
Why to always use for NSCharactorSet ?? this is simple and powerful solution
NSString *textStr = #"This is String Containing / Character";
if ([textStr containsString:#"/"])
{
NSLog(#"Found!!");
}
else
{
NSLog(#"Not Found!!");
You can use
NSString rangeOfString: (NSString *) string
See here: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html%23//apple_ref/occ/instm/NSString/rangeOfString:
If the NSRange comes back with property 'location' equal to NSNotFound, then the string does not contain the passed string (or character).

Resources