How to encode the string in to "windows-1252" using ios? - ios

The following string is working perfectly in android,please give me suggestion for encoding this in ios.
Android Example:String s = "hhh";
s.getBytes("Windows-1252");

A quick look at the docs for NSString would give you:
NSString *s = #"hhh";
NSData *data = [s dataUsingEncoding:NSWindowsCP1252StringEncoding];
uint_8 *bytes = [data bytes];

The equivalent code in iOS looks like this
NSString *str = #"hhh";
char buffer[100];
[str getCString:buffer maxLength:100 encoding:NSWindowsCP1252StringEncoding];

Related

want to have this string "مرحبا 😀 Hello" save it to database and display back to uilabel

I am looking for solution where i want to store English + Arabic + Emoji Character to store to Database and retrieve it back while display.
Below is the code what i have used to support Emoji, after that Arabic text is not showing.
+(NSString *)emojiToSave:(NSString *)str
{
NSData *dataForEmoji = [str dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *encodevalue = [[NSString alloc]initWithData:dataForEmoji encoding:NSUTF8StringEncoding];
return encodevalue;
}
+(NSString *)emojiToDisplay:(NSString *)str
{
NSData *msgData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodMsg = [[NSString alloc] initWithData:msgData encoding:NSNonLossyASCIIStringEncoding];
return goodMsg;
}
Can anyone pls suggest to give support for Arabic what change i should do?
Thanks in advance.
Try convert it into base64 code, then insert base64 code to database:
//Original string to base64 string
NSString *emojiString = #"مرحبا 😀 Hello";
NSData *emojiData = [emojiString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [emojiData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//Base64 string to original string
NSData *base64Data = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *originalString =[[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding];
NSLog(#"Result: %#",originalString);
Output:
You have to use an encoding that supports emoji and arabic characters. ASCII doesn't support that.
You should use NSUTF8StringEncoding everywhere, and you're fine.
Why are you using ASCII anyways? Why are you converting a string to an NSData and then back to NSString again? It doesn't make sense.

From NSString to escaped chars in iOS

I am not sure if I am asking the right question, please correct me if I'm wrong, but I've searched everywhere and I can't find an answer to help me with the following issue:
I have a NSString with the following content: "Björn Br. Björnsson"
and I need to get it to the following form: "Bj\u00f6rn Br. Bj\u00f6rnsson".
I've tried everything I found related on stackoverflow so far. If anyone has any idea how to get from ö type of characters to \u00f6 it would be awesome.
I have tried:
NSString *name = #"Björn Br. Björnsson";
NSString* string = [NSString stringWithCString:[name cStringUsingEncoding: NSUTF8StringEncoding] encoding:NSNonLossyASCIIStringEncoding];
or
NSMutableString *string = [[NSMutableString alloc] initWithString:name];
const char *encoded = [string cStringUsingEncoding:NSASCIIStringEncoding];
or
NSData* nsData = [name dataUsingEncoding:NSUTF8StringEncoding];
const char* data = [nsData bytes];
NSUInteger len = nsData.length;
NSMutableString* hex = [NSMutableString string];
for(int i = 0; i < len; ++i)[hex appendFormat:#"%02X", data[i]];
or
const char * encodedStringName = [crewmemName cStringUsingEncoding:NSISOLatin1StringEncoding];
and many other..
Thanks in advance.
You can create a category on NSString and call this method, this will encode to your desired way
#implementation NSString (URLEncoding)
- (NSString *) stringByUrlEncoding{
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)self, NULL, (CFStringRef)#"!*'();:#&;=+$,/?%#[]", kCFStringEncodingUTF8));
}
#end
NSString *name = #"Björn Br. Björnsson";
name =[name stringByReplacingOccurancesOfString:#"ö" withString:#"\u00f6"];
You may have to escape the backslash. Just type \\\ instead of \. But not sure about that.

How to concatenate TextView.text

I have two UITextViews:
self.itemsTextView.text;
self.priceTextView.text;
I want to concatenate these two like so:
NSString *data = self.textView.text + self.itemsTextView.text;
I have tried using a colon, as suggested by this thread, but it doesn't work.
NSString *data = [self.textView.text : self.itemsTextView.text];
For concatenating you have several options :
Using stringWithFormat:
NSString *dataString =[NSString stringWithFormat:#"%#%#",self.textView.text, self.itemsTextView.text];
Using stringByAppendingString:
NSMutableString has appendString:
You may use
NSString * data = [NSString stringWithFormat:#"%#%#",self.textView.text,self.itemsTextView.text];
There are so many ways to do this. In addition to the stringWithFormat: approaches of the other answers you can do (where a and b are other strings):
NSString *c = [a stringByAppendingString:b];
or
NSMutableString *c = [a mutableCopy];
[c appendString b];

sha-512 not working properly on IOS Simulator

I am implementing mobile app sha-512 function that will run on IOS, this function is working fine on all browsers including Safari.
I am testing my application using IOS Simulator.
My application is calling sha-512 function three times from one page/click. The problem is that, at first call sha-512 function produces right result, but at second and third call it produces wrong result.
thanks in advance
This is my code
//Creating Hash Value
NSString *hashkey = [NSString stringWithFormat:#"data"];
// PHP uses ASCII encoding, not UTF
NSLog(#"hashkey : %#",hashkey);
const char *s = [hashkey cStringUsingEncoding:NSASCIIStringEncoding];
NSData *keyData = [NSData dataWithBytes:s length:strlen(s)];
// This is the destination SHA512_Final
uint8_t digest[CC_SHA512_DIGEST_LENGTH] = {0};
// This one function does an unkeyed SHA1 hash of your hash data
CC_SHA512(keyData.bytes, keyData.length, digest);
// Now convert to NSData structure to make it usable again
NSData *out = [NSData dataWithBytes:digest length:CC_SHA512_DIGEST_LENGTH];
// description converts to hex but puts <> around it and spaces every 4 bytes
NSString *hash = [out description];
hash = [hash stringByReplacingOccurrencesOfString:#" " withString:#""];
hash = [hash stringByReplacingOccurrencesOfString:#"<" withString:#""];
hash = [hash stringByReplacingOccurrencesOfString:#">" withString:#""];
Hope it helps you..

How to convert NData populated with hex values to NSString

I have a NSdata object that is populated with a bunch of information thats formated in hex.. I am trying to convert it into its proper string representation but am struggling to have any success.
One thing I have tried is to simply put it into a NSString and then NSLog it with a special character identifier thingy.. forgot the word (%02x), However to do this I am encoding it to NSUTF16.. which i dont want to do.. I mearly want to see exactly whats the data I am getting looks like as a NSString.
The reason I am doing this is because I am having some issues with my encoding later on in my code and im not sure if its because the data I am receiving is incorrect or me stuffing it up at some point when I am handling it.
Any help would be appreciated.
You can get a string representation of your NSData like so:
NSData *data = (your data)
NSString *string = [NSString stringWithCString:[data bytes] encoding:NSUTF8StringEncoding];
Does that answer your question?
Maybe I haven't understood, but something like this:
NSData *yourData;
NSLog(#"%#", [yourData description]);
doesn't fit your need?
Give this a try -
-(NSString*)hexToString:(NSData*)data{
NSString *hexString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
if (([hexString length] % 2) != 0)
return nil;
NSMutableString *string = [NSMutableString string];
for (NSInteger i = 0; i < [hexString length]; i += 2) {
NSString *hex = [hexString substringWithRange:NSMakeRange(i, 2)];
NSInteger decimalValue = 0;
sscanf([hex UTF8String], "%x", &decimalValue);
[string appendFormat:#"%d", decimalValue];
}
return string;
}

Resources