• 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 Stream to String Conversion

 
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Please help me to resolve my problem.

I am getting byte stream as below. These looks like UTF 8 bytes

3C524F4F543E3C535452494E473E54455354204F4E4C5920535452494E473C2F535452494E473E3C2F524F4F543E



I want java code which will convert above bytes to string as shown below

<ROOT><STRING>TEST ONLY STRING</STRING></ROOT>





Much appreciate your valuable help.


Thanks
Raj
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a HEX encoded String. Convert it into a byte[] and then use one of the String constructors.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raj Smile wrote:Hi All,

Please help me to resolve my problem.

I am getting byte stream as below. These looks like UTF 8 bytes

3C524F4F543E3C535452494E473E54455354204F4E4C5920535452494E473C2F535452494E473E3C2F524F4F543E



I want java code which will convert above bytes to string as shown below

<ROOT><STRING>TEST ONLY STRING</STRING></ROOT>



Much appreciate your valuable help.

Thanks
Raj



No need to appreciate. Javaranch is not a code mill! You might get suggestions and ideas. It is you who has to come up with some code. Hope you were able to write what you wanted!
 
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

Raj Smile wrote:I am getting byte stream as below. These looks like UTF 8 bytes


Well, if they are, the standard approach to convert them to Java Strings or characters is normally to use a Reader (java.io.Reader), of which their are many flavours, the most basic one being InputStreamReader. if the stream contains "lines", you might also try a BufferedReader.

Winston
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now it is no longer All Fool's Day, I am not sure “beginning” is the right place for this discussion I shall move it: if you are lucky, into two places.

And welcome to the Ranch
 
Raj Smile
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell

And Yes Rob they are Hex encoded string.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So step 1: convert this HEX String into a byte[]. step 2: create a String around that byte[].
 
Raj Smile
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob, can you please give me a code snippet for this.


- Rohit
 
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

Raj Smile wrote:Rob, can you please give me a code snippet for this.


Well, assuming I'm now following this, and that
3C524F4F54...
stream is actually a sequence of bytes containing the ASCII (or UTF-8) characters '3', 'C', '5' and so on, there is more than one way to do it, but you'll still need a Reader to translate those "byte" characters to Java ones first.

Once you actually have the Java characters as a String (or Strings), you can then use either:
Byte.parseByte(String, radix) to convert them on an individual basis,
or possibly:
BigInteger(String, radix) to create a BigInteger from the whole thing,
and then:
BigInteger.toByteArray() to convert it to a byte array.

But you'll then need another Reader to convert the resulting bytes back to Java characters again.

HIH

Winston
 
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

Winston Gutkowski wrote:But you'll then need another Reader to convert the resulting bytes back to Java characters.


Actually, if you know that each of your "hex pairs" corresponds to a single character, you could probably just convert your bytes straight to characters by masking; but I suspect a Reader might be safer.

Winston
reply
    Bookmark Topic Watch Topic
  • New Topic