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);
Related
I have string
this string is encoded with euc-kr..
I try this code
NSString *str = MjAxObPitbUgx9C7/bq4yKPAzrfCKLnov/LFzcH2xbTAzCkguPDB/SCw+LDt
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:str options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:kCFStringEncodingEUC_KR];
but result is null..
what is wrong..? and I try
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:kCFStringEncodingDOSKorean];
and
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:kCFStringEncodingMacKorean];
but result is same..
encoding:kCFStringEncodingMacKorean's result is not korean.;;;;;
Here is a working solution:
Starting with:
NSString *str = #"MjAxObPitbUgx9C7/bq4yKPAzrfCKLnov/LFzcH2xbTAzCkguPDB/SCw+LDt";
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:str options:0];
What you missed is that you can't really use kCFStringEncodingEUC_KR which is a CFStringEncodings with initWithData:encoding: with expects a NSStringEncoding.
You can use CFStringConvertEncodingToNSStringEncoding() to convert it:
NSString *result = [[NSString alloc] initWithData:decodedData encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingEUC_KR)];
NSLog(#"result: %#", result);
Output:
$>result: 2019년도 학생보호인력(배움터지킴이) 모집 공고
If you want to use "natively" the kCFStringEncodingEUC_KR, and all the CFDataRef, CFStringRef, and use the bridge then to convert CFStringRef to NSString:
CFStringRef ref = CFStringCreateWithBytes(nil, [decodedData bytes], [decodedData length], kCFStringEncodingEUC_KR, false);
NSString *result = (__bridge NSString *)ref;
NSLog(#"result: %#", result);
Output:
$>result: 2019년도 학생보호인력(배움터지킴이) 모집 공고
You might need to do some changes, I'm not really familiar with CFStringCreateWithBytes(), check the documentation.
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];
I'm working on a bank application where we convert string into hexadecimal string and some time length of hexadecimal string is too long so i said to my senior developer to encode it into short string like as i mention in title and than decode it back to real string.
He said to me its not possible so i want to know is it possible or not?
And if its possible then how ?
He is using following code for encoding and decoding
NSString *plainString = #"foo";
Encoding
NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainData base64EncodedStringWithOptions:0];
NSLog(#"%#", base64String); // Zm9v
Decoding
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(#"%#", decodedString); // foo
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);