I have one function that returns me String :
public String getString(String password){
......
try {
.......
encodedPassword = Base64.encodeToString(msgDigest,1 );
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return encodedPassword;
}
I want to add (concatenate) "=" String to returning string from function
I try using this:
encrptdPassword = getString("1234");
encrptdPassword = encrptdPassword+"=";
Or:
encrptdPassword = encrptdPassword .concat("=");
but I get result like two different objects (space or brake between)
I think problem is in Base64.encodeToString , but I must use 64 based string
Function getString
returns me:
A6xnQhbz4Vx2HuGl4lXwZ5U2I8iziLRFnhP5eNfIRvQ
I want to add =
to the returning string as:
A6xnQhbz4Vx2HuGl4lXwZ5U2I8iziLRFnhP5eNfIRvQ=
but I receive this on output
A6xnQhbz4Vx2HuGl4lXwZ5U2I8iziLRFnhP5eNfIRvQ =
Or:
A6xnQhbz4Vx2HuGl4lXwZ5U2I8iziLRFnhP5eNfIRvQ
=
...like 2 different strings.
Where I'm wrong?
-
1Please post an example of your input and of the wrong result that you are getting. Otherwise it is not clear what is the problemYoni– Yoni2011年03月21日 18:59:58 +00:00Commented Mar 21, 2011 at 18:59
-
1I don't understand. Please paste exactly what output you're getting and exactly what output you're expecting.Wayne– Wayne2011年03月21日 19:00:38 +00:00Commented Mar 21, 2011 at 19:00
-
Is this Android? You'd get a lot better answers if you pointed that out or what library you're using for the Base64 class.Joseph Erickson– Joseph Erickson2011年03月22日 18:46:43 +00:00Commented Mar 22, 2011 at 18:46
2 Answers 2
I assume you're using Base64 from Apache Commons Codec.
The default constructor for this class uses "\r\n" as a line separator, which it adds to the end of every encoded line. If you don't want this, construct the object as:
new Base64(76, '');
If this isn't the class you're calling (it looks like from your code sample you're calling a static method), check the API and see if you can set a line separator for the conversion.
-
can you please post me code ? msgDigest is byte array (byte[])Jovan– Jovan2011年03月21日 19:11:36 +00:00Commented Mar 21, 2011 at 19:11
Isn't the 1 in Base64.encodeToString(msgDigest,1 ) padding?
If it's not, then you could just trim() the string to remove the whitespace.
-
1@Jovan, maybe trim worked, but I wouldn't rely on it. If the space or carriage return were there before the concatenation, I'd want to understand why. What implementation of base64 are you using?Yoni– Yoni2011年03月21日 20:11:30 +00:00Commented Mar 21, 2011 at 20:11