• Post Reply Bookmark Topic Watch Topic
  • New Topic

Byte Stream to String Conversion

 
Raj Smile
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a HEX encoded String. Convert it into a byte[] and then use one of the String constructors.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • 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!
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Campbell

And Yes Rob they are Hex encoded string.
 
Campbell Ritchie
Marshal
Posts: 52581
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Rob, can you please give me a code snippet for this.


- Rohit
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!