• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object > byteArray > String > byteArray > Object  RSS feed

 
Karsten Daemen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question:

Is their way to convert a byte array to a string, and convert that string back to te exact byte array from therefore?

Purpose

I'm trying to get this script working:

So object2 is a clone of object1.

Why would I want to do that?

I want to save/serialise the objects on a remote webserver, the only way of passing information with the server and me is through a query string (after ? in the url). The webserver will save the string in a textfile so I can read it afterwards and create the object again.

Can you help me?

Thanks in advance!
 
Paul Clapham
Sheriff
Posts: 22697
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, hopefully this is a pretty small object, as a lot of systems restrict the length of a URL quite considerably. But here's your problem: converting an array of bytes to a String the way you did assumes that those bytes represented a String in the first place. That they represented text, in other words. But they don't, and when the conversion is done assuming text, things are changed to make the result look like text.

What you should do is to convert the array of bytes to a string using Base64 encoding. That works in a reversible way, although the size of the result is 33% larger than the size of the input, so that puts you even more in danger of having your URL truncated. There isn't a class to do that in the standard API but you can find Java Base64 implementations on the internet.
 
Karsten Daemen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Well, hopefully this is a pretty small object, as a lot of systems restrict the length of a URL quite considerably. But here's your problem: converting an array of bytes to a String the way you did assumes that those bytes represented a String in the first place. That they represented text, in other words. But they don't, and when the conversion is done assuming text, things are changed to make the result look like text.

What you should do is to convert the array of bytes to a string using Base64 encoding. That works in a reversible way, although the size of the result is 33% larger than the size of the input, so that puts you even more in danger of having your URL truncated. There isn't a class to do that in the standard API but you can find Java Base64 implementations on the internet.


Thank you very much for this information, it's really good! I'm kind of new to this kind of programming ...

Do you know another way of sending those data? I use cgi/perl scripts on the server to get the job done.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Commons Codec project from Apache might do the work:

In the encoding side, you could do somewhat like this:


And at the decoding side, somewhat like this:



Hope that helps!
 
Karsten Daemen
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edwin Dalorzo wrote:The Commons Codec project from Apache might do the work:

In the encoding side, you could do somewhat like this:


And at the decoding side, somewhat like this:



Hope that helps!


Thank you! That was really helpfull!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!