Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Base64 decode

Status
Not open for further replies.

Pichdude

Programmer
Aug 24, 2005
66
SE
Hi,

I am using these two methods to make a Base64-encoded String from a regular text. First convert it to a byte[] and then to Base64 encoding.

Code:
String originalText = "abc123";

String base64Text = toBase64(originalText);

private String toBase64(String s) throws SystemException {
	return new BASE64Encoder().encode(toByteArray(s));
}

private byte[] toByteArray(String s) throws SystemException {
	try {
		return s.getBytes("UTF-8");
	} catch (UnsupportedEncodingException e) {
		SystemException se = new SystemException();
		throw se;
	}
}

My problem is, later on I need to use the original text again. Can anybody help me on how I get back from the Base64-encoded text "YWJjMTIz" to "abc123"?

 
From where do i get that class and method?

Regards

Per
 
Nevermind.

I think my problem is that before I Base64-encode I have to make the String into a byte[], and than later when I decode the Base64-encoded String i receive the byte[]. But then I want to go back from the byte[] to the exact original String ("acb123"). How do I do that?

Regards

Per


Ps. I use:
sun.misc.BASE64Encoder().encode(byte[] arg)
sun.misc.BASE64Decoder().decodeBuffer(String arg)

Method decodeBuffer() returns a byte[]. Can find any methods for encoding and decoding that takes and returns String.
 
Ah, I see, I wasn't understanding the question.

You just need the constructor for String object

Code:
byte[] myBytes;
String myString = new String(myBytes,0,myBytes.length,"UTF-8");

Cheers,
Dian
 
I know :p

Btw, remember what I told you in the other post abut the standard Java forum.

Cheers,
Dian
 
Has any body heard if there is a max size of how long the String can be that is to BASE64 encoded by sun.misc.BASE64Encoder?
 
I don't believe there is a max size per se in the Base64Encoder class.

Some of my code successfully encodes/decodes up to 5/6/7 MBs ...

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
After about 56 to 57 characters i get "\r\n" inserted to the code. After the next 56-57 characters I get it again, and so on.. Have you any idea why?

Regards
 
Yeah, I get that too, I wouldn't worry about it - it'll make no difference (or shouldn't) to the Decoder, because Base64 does not include control characters such as CRLF.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Aha, then there is nothing wrong with my code. The problem is that i publish the base64 encoded string on a webpage, and there the \r\n makes a line break and gives me problem. But for now I just replace them characters in the base64 encoded string with nothing, and problem solved.
 
It a appears that BASE64 does this to provide nice print outs and it should not effect the code. But since it does for me do anybody know if there is a way to set after which character BAS64 should insert these line breaks? If so, then I could set it to 100 and it would not be a problem for me, since my Strings are not longer than around 80.

Regards

Pichdude
 
Just do :

String base64encoded = Base64.encode...
base64encoded = base64encoded.replaceAll("\r\n", "");

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Yes that is what I do. But that is a rather "ugly" solution. :) If it where possible to control it from the BASE64Encoder it would be more correct.
 
Well that it might my good fellow, but considering that the class BASE64Decoder is neither documented, nor is the source shipped, I don't like your chances.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top