i am not able to decode this encoded string to its unicode value:- %5Cu20A8
NSData *unicodedStringData =
[#"%5Cu20A8" dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];
NSString *stringValue =
[[NSString alloc] initWithData:unicodedStringData encoding:NSUTF8StringEncoding];
I have tried NSASCIIStringEncoding and NSUnicodeStringEncoding as well but it doesnt work.
This Worked:-
NSString *unicodedString = [[dictMain objectForKey:#"currency"] stringByRemovingPercentEncoding];
NSData *unicodedStringData = [unicodedString dataUsingEncoding:NSUTF8StringEncoding];
NSString *currencyUnicode = [[NSString alloc] initWithData:unicodedStringData encoding:NSNonLossyASCIIStringEncoding];
Just removing percent escapes before decoding the string.
Just improved Ashish's answer
NSString *unicodedString = [#"%5Cu20A8" stringByRemovingPercentEncoding];
NSData *unicodedStringData = [unicodedString dataUsingEncoding:NSUTF8StringEncoding];
NSString *currencyUnicode = [[NSString alloc] initWithData:unicodedStringData encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"%#",currencyUnicode);
Output - ₨
Related
I am trying this.
-(NSString *)convertEmojiIntoUnicode:(NSString *)emojiStr{
NSData *data = [emojiStr dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return goodValue;
}
For Decoding
-(NSString *)convertUnicodeToEmoji:(NSString *)unicodeStr{
NSString *newString = unicodeStr;
NSData *data1 = [newString dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodValue1 = [[NSString alloc] initWithData:data1 encoding:NSNonLossyASCIIStringEncoding];
return goodValue1;
}
for characters like è is converting to \350 with above method but I am getting unicode for è- \u00E8 from android and website.
I can decode \350 and \u00E8 both with above decode method(convertUnicodeToEmoji) to è
Simply try out this solution :
NSString *myString = #"Here is conversion of char ë, é";
NSData *mData = [myString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *strResult = [[NSString alloc] initWithData:mData encoding:NSASCIIStringEncoding];
NSLog(#"Result: %#", strResult);
Here is Output :
Result: Here is conversion of char e, e
Hope it will work for you.
Im using the following code to convert base64 string to ordinary string.
NSError *localError = nil;
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:myString options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(#"encoded string , %#",myString);
NSLog(#"Decode String Value: %#", decodedString);
Encoded string prints the base64 string but the decoded string is empty. Why so?
Avoid converting to a string before decoding:
NSData *decodedData = [[NSData alloc] initWithBase64EncodedData:data options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
Some implementations of Base64 add line breaks every 64 characters. You should be able to address this by using this option: NSDataBase64DecodingIgnoreUnknownCharacters.
If you want to encode and decode data then you can use this code.
// Create NSData object
NSData *data1 =[#"My String" dataUsingEncoding:NSUTF8StringEncoding];
// Encoded NSString from NSData
NSString *base64Encoded = [data1 base64EncodedStringWithOptions:0];
NSLog(#"%#",base64Encoded);
// Encoding data
NSData *base64Data = [data1 base64EncodedDataWithOptions:0];
NSLog(#"%#",base64Data);
// Decoding data
NSData *nsdataDecoded = [base64Data initWithBase64EncodedData:base64Data options:0];
NSString *str = [[NSString alloc] initWithData:nsdataDecoded encoding:NSUTF8StringEncoding];
NSLog(#"%#", str);
You can use Base64 library to encode or decode,
https://github.com/dasdom/hAppy/tree/master/base64
Then you can use this code,
NSString *strEncoded = [Base64 encode:data];
How do i turn the following into an emoji smiley face?
\uD83D\uDE04
According to Unicode Wiki, that is a "C/C++/Java source code". However when I save that as a NSLabel it just does exactly that. The text is \uD83D\uDE04 and not the emoji expected.
Try this code sample. It will help you.
NSString *str = #"\uD83D\uDE04";
NSData *data = [str dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *valueUnicode = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *dataa = [valueUnicode dataUsingEncoding:NSUTF8StringEncoding];
NSString *valueEmoj = [[NSString alloc] initWithData:dataa encoding:NSNonLossyASCIIStringEncoding];
uiTextLable.text = valueEmoj;
I have fetched data from sqlite database using CoreData, and got NSString value:
"SxzYEWe6vE2ggLy5k9XV0Q24QJoOfvrHt9jCpq8CgCkGvfdgghjPIhDH0qZkNXjSHQJEw0cbGzFEoqZSIpImoqcuc8iA57oRne31fxgigPLWiAyAjjNnam68Z25hArGzDXudEA1AXoSM1TmlLMSV6c3XfDZM/IPPR245K5+DawOH5M8ZdUnbDyqVKRsi6KvqKou7mVA8DoZWrPBgN5YbvVC/da0F9QHCcq8Di8dNQ2/sf/oVs/A+ThHnglkscKyFuQ3FH+HNjENG2ZHtyWe1fQ=="
It seemed to be a base64 encoded string. I tried many ways to decode the string using:
NSData *dataString = [[NSData alloc] initWithBase64EncodedString:data.desc options:0];
NSString *textMeaning = [[NSString alloc] initWithData:dataString encoding:NSUTF8StringEncoding];
// textMeaning return (null).
has anyone seen this encoded string before and know how to decode it?
Try this base64 method to decode,
NSString *decodeString = #"Apple";
//Encode String
NSData *encodeData = [decodeString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [encodeData base64EncodedStringWithOptions:0];
NSLog(#"Encoded String Value: %#", base64String);
//Decode String
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(#"Decoded String Value: %#", decodedString);
I am getting encoded string.string contains '=' sign at last. I am trying to decode it,using Base64
but its not decode properly.Here is my code:
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:string options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(#"decodedString %#", decodedString);
but, decodedString is nil.Please help me.Thanks in advance.
The only logical explanation I can see is that the string that you are decoding is empty because that is the correct way to decode a base64 encoded.
May be you are encoding it wrong. Try this code, its working fine and tested:
NSString *string = #"user3007459=";
NSString *base64EncodedString = [[string dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64EncodedString options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(#"decodedString %#", decodedString);