Decode the special symbols like html tags $ #
response jsonString
"value": "15000",
"price": "\u20b9125.00\/-",
"href": "http:\/\/abce\/abcd\/index.php?route=abcd\/product&id=abcd"
// Converting price into $ I am using below code, but decodedString is showing nil.
NSString *jsonString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSData *unicodedStringData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSString *decodedString = [[NSString alloc] initWithData:unicodedStringData encoding:NSNonLossyASCIIStringEncoding];
I got response data, after decode its give an nil value.
Can any one advice me to resolved the issue.
#All Thanks in advance.
Try below code.
NSString *jsonString = [[NSString alloc] initWithData:urlData encoding:NSASCIIStringEncoding];
NSData *unicodedStringData = [jsonString dataUsingEncoding:NSASCIIStringEncoding];
NSString *decodedString = [[NSString alloc] initWithData:unicodedStringData encoding:NSNonLossyASCIIStringEncoding];
Edited, Try below code,
NSString *decodedString = [[NSString alloc] initWithData:unicodedStringData encoding:NSASCIIStringEncoding];
Regards,
Amit
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.
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 - ₨
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 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);