Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Converting ASCII text (byte array ending in null) to a String

807589Jul 12 2008 — edited Jul 12 2008
Hi all,

I am trying to convert a null terminated ascii string stored in a byte array to a java String. When I pass the byte array to the String constructor it
does not detect/interpret the null as a terminator for the ascii string. Is this something I have to manually program in? ie Check for where the null is and then
pass that subarray of everything before the null to the String constructor?


example of problem
//               A   B  C   D   null   F   (note F is junk in the array, and should be ignored since it is after null)
byte[] asciiArray = { 65, 66, 67, 68, 0,  70 };

System.out.println(new String(asciiArray, "UTF-8"));
//this prints ABCD"sqare icon"F
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 9 2008
Added on Jul 12 2008
2 comments
622 views