• 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

byte to String conversion

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to convert Strings to and from bytes. I'm running into a problem converting from bytes to Strings. If I use the following code:

How do I recreate the original String with a length of 2 and that only contains "Ab"?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your second line is not permissible in Java, since you have already declared b to be a byte[]; it cannot be a string. Have you compiled this?

In order to answer your question, try this:

As far as why your String contains 'A', 'b', and 0, remember:
1). Strings are not null-terminated arrays in Java
2). Arrays are initialized to zeros (this mening changes depending on data type; object arrays are, of course, initialized to null (not necessarily zero, but the logical equivilent) and boolean arrays to false (again, not necessarily zero, but the logical equivilent)
Thus, if you had:

This will produce:
Ab:length:3
65
98
0
(The 0 does not print when the string prints because, as a char, it still is a NPC; however, as evidenced by the length of the string, it still takes up a position in the string because Java does not recognize it as a terminating character)
Gee, this is turning into a dissertation
Final thought for you: what does this do?
 
Ron Aronica
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the comments Joel,
You are correct about the code. I wrote it incorrectly; it should have had .getBytes().
I understand your example method for addressing the problem. I was hopeing that there is an API call about which I am unaware that solves the problem. I am somewhat surprised that there is not one as this seems to be a relatively common thing to want to do.
 
Joel McNary
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know how common it is, actually. I have never had occassion to need to do this. I just use the String's .charAt() method to read the String and a StringBuffer if I need to build/modify a String. Perhaps others can comment on if/how they use byte arrays?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some simpler ways to accomplish this:

or
 
Ron Aronica
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim,
I did not realize that trim() considers '0' white space.
reply
    Bookmark Topic Watch Topic
  • New Topic