The moose likes Beginning Java and the fly likes String(byte[ ]) constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String(byte[ ]) constructor" Watch "String(byte[ ]) constructor" New topic

String(byte[ ]) constructor

Jessica Farnham

Joined: Feb 12, 2001
Posts: 5
For an application I'm working on, I'm trying to convert a byte array to a String for storage in a database. I've been trying to use a constructor I found in the String class documentation: String(byte[]bytes, String encoding). My code is compiling and running with no errors, but the resulting String is always empty, even though I've confirmed through debugging code that the byte array definitely has data. I suspect that the problem may be related to the fact that some of the bytes are negative or in some way out-of-range for the encoding, but haven't been able to identify the exact problem. Does anybody know how to make this work?
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4351

I have never had to do that. this info from the API might be helpful(for the String you supply to the constructor).

Every implementation of the Java platform is required to support the following character encodings. Consult the release documentation for your implementation to see if any other encodings are supported.
US-ASCII Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
ISO-8859-1 ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
UTF-8 Eight-bit Unicode Transformation Format
UTF-16BE Sixteen-bit Unicode Transformation Format, big-endian byte order
UTF-16LE Sixteen-bit Unicode Transformation Format, little-endian byte order
UTF-16 Sixteen-bit Unicode Transformation Format, byte order specified by a mandatory initial byte-order mark (either order accepted on input, big-endian used on output)

Visit my download page
I agree. Here's the link:
subject: String(byte[ ]) constructor
jQuery in Action, 3rd edition