base64 decode string and encode all special characters lost - android

I have a Java class which I convert to string using GSON. Post this the string is base64 encoded (for some reason, lets not go there :) ) When I decode it back I lose all { and " " characters in json.
For example: {"name":"ABC"} decoded and encoded back becomes nameABC
I want to get my old data back i.e I want {"name:"ABC"} back
String json = "{\"name\":\"ABC\"}";
byte en[] = android.util.Base64.decode(json,Base64.NO_WRAP);
String st = android.util.Base64.encodeToString(en,Base64.NO_WRAP);
Something as simple as above, content is lost
Please help

You can't Base64 has set 64 characters that can converted to binary and vice versa, characters like { and " is not in the 64 set of characters check this
Try using URLDecoder with UTF-8 or any other encoding method which support UTF-8

Related

I get fields of type String with some Unicode characters instead of the usual

My problem is that I am getting strings where some characters are Unicode.
"fieldName": "Ac6jHguQjKKUxx6MSOpjO2kOLKPAdjStVs1pgTGNSU8\u003d"
Then I immediately send such a string to another API and the server returns me an error with a code of 500. If I use this string in postman and replace the unicode with a normal one, then the code 200 is returned from the server.
I thought there was a problem in the server, but they checked it and said that they were sending it as expected.
How do I translate Unicode?
The easiest way is to use URLDecoder. Here is an example.
String str = "Ac6jHguQjKKUxx6MSOpjO2kOLKPAdjStVs1pgTGNSU8\u003d";
String decode = URLDecoder.decode(str, "UTF-8");
System.out.println(decode);
//Ac6jHguQjKKUxx6MSOpjO2kOLKPAdjStVs1pgTGNSU8=

Encode string to Base64 in android without special character?

I am trying to encode the string data in Base64. I am using this code :
Byte[] url_base64=repl.getBytes("UTF-8");
String con_base64=Base64.encodeToString(url_base64, Base64.NO_WRAP);
Via this code i am getting special characters like ("/","\","\n") etc. How to ignore these special characters in base64 encoding in android.
Please help me if you have any idea about it.
Not sure if this is what you want, but
Base64.encodeToString(url_base64, Base64.NO_WRAP | Base64.URL_SAFE);
will replace "\" with "_"

Bullet proof way to get string to test encoding

I am having utf-8 encoding trouble and was looking for a way to create a test string that was known to contain at least one utf-8 character. Ideally I would like to have a string contain a lower-case e with an acute. This is unicode 00e9 which should be encoded as a byte C3 followed by a second byte A9.
Imagine that I can not guarantee that the encoding in my editor is correct - so I guess I need to somehow create a byte array and covert to a string?? Not sure - please advise.
I want the string for loading into a webview like so:
webView.loadData(test_string, "text/html", "UTF-8");
You can encode unicode by using the \u escape. Since ICS loadData does not seem to work with encoded strings, use loadDataWithBaseURL passing null for first and last params:
String test_string="One e and another type \u00E9";
webView.loadDataWithBaseURL(null, test_string, "text/html", "UTF-8", null);

Android convert ISO-8859-2 to UTF-8

I'm downloading HTML source code of remote page into String variable. Unfortunetely the page is encoded via iso-8859-2 and contains characters from polish alphabet. How can I convert this string to utf-8, so I can display it's parts in TextView?
Thanks
You shouldn't need to "convert" the string at all, if you obey the Content-Encoding header sent by the web server.
Right now, you probably ignore that header while reading the response from the server (some BufferedReader-to-StringBuffer/Builder loop I assume), try this in your download code instead:
HttpResponse response = ....
String text = EntityUtils.toString(response.getEntity());
EntityUtils will automagically use the content encoding specified by the server.

android java URLDecoder problem

i have a String displayed on a WebView as "Siwy & Para Wino"
i fetch it from url , i got a string "Siwy%2B%2526%2BPara%2BWino". // be corrected
now i'm trying to use URLDecoder to solve this problem :
String decoded_result = URLDecoder.decode(url); // the url is "Siwy+%26+Para+Wino"
then i print it out , i still saw "Siwy+%26+Para+Wino"
Could anyone tell me why?
From the documentation (of URLDecoder):
This class is used to decode a string which is encoded in the application/x-www-form-urlencoded MIME content type.
We can look at the specification to see what a form-urlencoded MIME type is:
The form field names and values are escaped: space characters are replaced by '+', and then reserved characters are escaped as per [URL]; that is, non-alphanumeric characters are replaced by '%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks, as in multi-line text field values, are represented as CR LF pairs, i.e. '%0D%0A'.
Since the specification calls for a percent sign followed by two hexadecimal digits for the ASCII code, the first time you call the decode(String s) method, it converts those into single characters, leaving the two additional characters 26 intact. The value %25 translates to % so the result after the first decoding is %26. Running decode one more time simply translates %26 back into &.
String decoded_result = URLDecoder.decode(URLDecoder.decode(url));
You can also use the Uri class if you have UTF-8-encoded strings:
Decodes '%'-escaped octets in the given string using the UTF-8 scheme.
Then use:
String decoded_result = Uri.decode(Uri.decode(url));
thanks for all answers , i solved it finally......
solution:
after i used URLDecoder.decode twice (oh my god) , i got what i want.
String temp = URLDecoder.decode( url); // url = "Siwy%2B%2526%2BPara%2BWino"
String result = URLDecoder.decode( temp ); // temp = "Siwy+%26+Para+Wino"
// result = "Swy & Para Wino". !!! oh good job.
but i still don't know why.. could someone tell me?

Categories

Resources