aspose file tools*
The moose likes Java in General and the fly likes Converting byte[] to String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Converting byte[] to String" Watch "Converting byte[] to String" New topic
Author

Converting byte[] to String

Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 580

This returns the address of the byte array. I want to display the string as a UTF8 or ASCII character string.
Thanks
Jack
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4244
    
    7

Have a look at the constructors available in the String class - one of those will probably do what you want.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Jacky Luk wrote:I want to display the string as a UTF8 or ASCII character string.

My advice then: Use UTF-8, since for the values 0-127, it is the same as ASCII. They are NOT the same thing, however.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 580
Matthew Brown wrote:Have a look at the constructors available in the String class - one of those will probably do what you want.



If the byte array consists of 72,101,108,108,111,-112,0
And you see this is a string "hello" with a -112 at the end
How do I skip the -112 character and return 72,101,108,108,111,0?
Thanks
Jack
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Note that Java byte values run 0 to 255, values above 127 will print as negative integers so that last character is above the ascii 7 bit character set.

How do I skip the -112 character and return 72,101,108,108,111,0?


I thought you wanted a string consisting of the characters corresponding to 72,101, etc - do you really want a list of byte values as a String?

Bill

Java Resources at www.wbrogden.com
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 580
William Brogden wrote:Note that Java byte values run 0 to 255, values above 127 will print as negative integers so that last character is above the ascii 7 bit character set.

How do I skip the -112 character and return 72,101,108,108,111,0?


I thought you wanted a string consisting of the characters corresponding to 72,101, etc - do you really want a list of byte values as a String?

Bill


Hi Will,
I want a list of byte values as a string. As the library I am using returns a array of bytes with a -127 character on the end. I'd like to eliminate that byte.
That byte I believe was part of the communication protocol. I want to strip that.
Thanks
Jack
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

Jacky Luk wrote:I want a list of byte values as a string.


Okay...

As the library I am using returns a array of bytes with a -127 character on the end. I'd like to eliminate that byte.
That byte I believe was part of the communication protocol. I want to strip that.


If that's the actual requirement, then the decision to change the array of bytes to a String was the wrong decision. Just create a new byte array which is one entry shorter than the original array, and copy the original array (except the last entry) into the new array. This has nothing to do with text processing, so using Strings is wrong.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213

At Jack:

if you want to just convert the values of byte array into String then try this.



OCPJP
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 580

Guess what, I get a string of things like
72101108108111
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213

Jacky Luk wrote:
Guess what, I get a string of things like
72101108108111


Like what kind of output do you want now?
Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 580
Ishan Pandya wrote:
Jacky Luk wrote:
Guess what, I get a string of things like
72101108108111


Like what kind of output do you want now?


I want the string "hello" with the last 2 bytes stripped off.
Thanks
Jack
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213

This would definitely help out.



Jacky Luk
Ranch Hand

Joined: Aug 02, 2012
Posts: 580

Very clumsy. But it works
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Well, it's what you asked for:
Jacky Luk wrote:If the byte array consists of 72,101,108,108,111,-112,0
And you see this is a string "hello" with a -112 at the end
How do I skip the -112 character and return 72,101,108,108,111,0?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Jacky Luk wrote:That byte I believe was part of the communication protocol. I want to strip that.

Well, you shouldn't guess about these things, so before you do, make sure that it is.

However, the simplest way to do that is:
byte[] res = Arrays.copyOf(res, res.length() - 1);

and as far as converting from UTF-8 is concerned, have a look at the String constructor that takes a Charset.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting byte[] to String
 
Similar Threads
Converting a Blob to a String for display
==, why is it so ?
a simple question
Trouble with system.in manipulation
Reading image and convert it to Base64 format