Encode string to Base64 in android without special character? - android

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 "_"

Related

base64 decode string and encode all special characters lost

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

Bad base-64 error

I'm getting a "java.lang.IllegalArgumentException: bad base-64" on the following code:
byte[] msgBytes = Base64.decode(msgStr, Base64.NO_WRAP);
msgString is a String, and right before this line, I check the value of msgStr and it is "fl-ILw==". Is there anything wrong?
Thanks.
According to RFC 4648(http://www.rfc-editor.org/rfc/rfc4648.txt) '-' character is not a valid Base64 character but on the other hand is valid for "URL and Filename safe Base 64 Alphabet".
So you could use Base64.URL_SAFE depending of the expected format of the string.

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

In Android how to concatenate base64 encoded strings?

Please help me to solve this:
I have two strings for email-id and password like
String name = "xyz#gmail.com";
String pass = "abc";
I encode these two into Base64 string like
String encoded_name = new String(Base64.encode(name.getBytes(), 0));
String encoded_pass = new String(Base64.encode(pass.getBytes(), 0));
and I need to concatenate these two encoded strings with space like
String merge = encoded_name + " " + encoded_pass;
I checked this string in console by
System.out.print("Concatenate string= " + merge);
but in console I am getting result in two lines like this
11-18 00:25:29.898: INFO/System.out(1244): Merge= eHl6QGdtYWlsLmNvbQ==
11-18 00:25:29.908: INFO/System.out(1244): YWJj
Why is this happing the result is unexpected for me why it is not printing in a single line. please help me to solve this.
Thanks
You should use the NO_WRAP flag as described in the Docs, the Base64 class will not add additional newlines.
NO_WRAP: Encoder flag bit to omit all line terminators (i.e., the output will be on one long line).
So change your lines to the following:
String encoded_name = new String(Base64.encode(name.getBytes(), Base64.NO_WRAP));
String encoded_pass = new String(Base64.encode(pass.getBytes(), Base64.NO_WRAP));
This will output the following:
11-17 19:16:51.283: INFO/System.out(354): Concatenate string= eHl6QGdtYWlsLmNvbQ== YWJj
Please have a look on Android Api reference Document. It will solve all other queries regarding Base64 encoding/decoding in android.
one more efficient way to solve your problem:
String encoded_name = Base64.encodeToString(name.getBytes("utf-8"), Base64.NO_WRAP);
String encoded_pass = Base64.encodeToString(pass.getBytes("utf-8"), Base64.NO_WRAP);
out put:-
11-17 10:55:27.492: V/BASE-64-.encodeToString(525): eHl6QGdtYWlsLmNvbQ== YWJj
Look at this doc, the mime section states that
MIME does not specify a fixed length for Base64-encoded lines, but it does specify a maximum line length of 76 characters. Additionally it specifies that any extra-alphabetic characters must be ignored by a compliant decoder, although most implementations use a CR/LF newline pair to delimit encoded lines.
You should only consider removing the last char of your first base 64 string, it seems to be a \n (a more generic method would be to test wether it is or not.)
Regards,
Stéphane

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