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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting an array of bytes into a (usable) string

Status
Not open for further replies.

carpeliam

Programmer
Mar 17, 2000
990
US
I have an array of bytes, and I want to convert this into a string containing just the characters A-Z, a-z, and 0-9. I'm wondering what the recommended way to do this might be. I'm sure there are many ways to do it, But I'm looking for the best possible way, and I also don't want to reinvent the wheel.

I'm using jdk1.4, if that makes a difference. Thanks:) Liam Morley
lmorley@gdc.wpi.edu
"light the deep, and bring silence to the world.
light the world, and bring depth to the silence."
 
thank you for your reply. the problem with that is, certain characters (like "/") are within the default charset which I can't handle.

Eventually, this string will wind up within a URL in the form
Code:
myurl.com/page.html?var=ABCabc123
, which is why I need to restrict the charset.

I've come up with a few ideas.[ol][li]there are 62 (26 x 2 + 10) characters that fall within my character set. convert the char value of each byte into a number between 1 and 62, and use that as the index for an array (where the array[0] = 'A', array[26] = 'a', array[52] = '0', etc).[/li][li]Construct a new java.nio.charset.Charset. This would only work within 1.4, So I'm not terribly excited about doing that from a portability standpoint (As this is open-source code which will be run/compiled by others). I like it simply because it makes sense to use the String(byte[] bytes, String charsetName) constructor approach.[/li][li]either disregard or generate new values for offending bytes. Disregarding is bad (who knows, I might get an empty string back), and who knows how long it might take to generate new values.[/li][/ol]I'm not a huge fan of any of these plans.. If it wasn't deprecated, I'd use something like String(byte[] ascii, int hibyte).

I'm getting the bytes from SecureRandom. Is there any way to limit the output on that? Or is there a better way of generating a random string of numbers and letters?

Thanks again for your time. Liam Morley
lmorley@gdc.wpi.edu
"light the deep, and bring silence to the world.
light the world, and bring depth to the silence."
 
I came up with a solution. I remembered that servlet container session id's have much the same charset.. so I did a little research on how they are constructed. In case you're curious, it seems to be as follows:

Code:
java.net.URLEncoder.encode(new java.rmi.server.UID().toString())

thanks in any case.:) Liam Morley
lmorley@gdc.wpi.edu
"light the deep, and bring silence to the world.
light the world, and bring depth to the silence."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top