how to decode base64 NSString(using euc-kr) .. T.T - ios

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.

Related

special character like è,á are not converting their respective unicodes in Ios for other platform like android and app website

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.

Base 64 decoding produces empty

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];

Decoding an encoded string

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);

How to decode string with Base64 in iPhone

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);

'initWithBase64Encoding' is deprecated?

I want to get the data from the string. i am using the following code but it seems to be deprecated.
NSData *data=[[NSData alloc]initWithBase64Encoding:(NSString *)dict];
I got the data. But its give me the warning that 'initWithBase64Encoding' is deprecated.
So is there any other method that will return data?
use this
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
instand of
NSData *data=[[NSData alloc]initWithBase64Encoding:(NSString *)dict];
Returns a data object initialized with the given Base-64 encoded string. (Deprecated in iOS 7.0. You should transition to either initWithBase64EncodedString:options: or initWithBase64EncodedData:options:.)
Use following
ENCODE
NSString *myString = #"Developer";
NSData *myData = [myString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [myData base64EncodedStringWithOptions:0];
NSLog(#"%#", base64String);
DECODE:
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(#"%#", decodedString);

Resources