• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting byte[] to String

 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4565
8
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the constructors available in the String class - one of those will probably do what you want.
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20719
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At Jack:

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

 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Guess what, I get a string of things like
72101108108111
 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would definitely help out.



 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Very clumsy. But it works
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Winston Gutkowski
Bartender
Pie
Posts: 10087
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic