Convert hexadecimal into short hexadecimal string like "Zx4E5" - ios

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

Related

Not able to decode the encoded unicode string

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 - ₨

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

iOS filter out characters that cannot be encoded

Hi I am developing an iOS application and I am facing the following question.
I have a NSString that contains Traditional Chinese, Simplified Chinese and probably some special characters like "®".
When I want to convert this NSString to NSData using
[string dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingBig5)]
the NSData returned is nil and I want to remove the invalid characters that cannot be encoded.
How can I do this? Thanks!!
NSString *string = #"®한자";
NSString *newStr = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *data = [newStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *originalString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] stringByRemovingPercentEncoding];

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

Resources