• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Converting byte[] to String

 
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Guess what, I get a string of things like
72101108108111
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 228
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This would definitely help out.




 
Jacky Luk
Ranch Hand
Posts: 634
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Very clumsy. But it works
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic