Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Byte Stream to String Conversion

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Byte Stream to String Conversion

    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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Byte Stream to String Conversion

    What type of data is the byte stream in? What you have posted looks like a String.
    Are you asking how to convert a String of hex characters into another String where each 2 hex characters is converted to a character?
    The methods:
    substring() to get each 2 hex characters
    an Integer class method to convert that 2 char String into a int
    cast to get the char value from that int
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Raj4Smile (April 17th, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Byte Stream to String Conversion

    Hi Norm, this is resolved.

    Thanks
    Raj

  5. #4

    Default Re: Byte Stream to String Conversion

    import java.io.UnsupportedEncodingException;


    public class CrunchifyByteArrayToString {
    public static void main(String[] args)
    {
    String testString = "Crunchify Example on Byte[] to String";
    byte[] bytesData = testString.getBytes();

    System.out.println("testString : " + testString);
    System.out.println("\nbytesData : " + bytesData); // .getBytes on String will return Hashcode value
    System.out.println("bytesData.toString() : " + bytesData.toString()); // .toString() will return Hashcode value

    String decodedData = new String(bytesData); // Create new String Object and assign byte[] to it
    System.out.println("\nText Decryted : " + decodedData);
    String decodedDataUsingUTF8;
    try {
    decodedDataUsingUTF8 = new String(bytesData, "UTF-8"); // Best way to decode using "UTF-8"
    System.out.println("Text Decryted using UTF-8 : " + decodedDataUsingUTF8);
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }
    }
    }[COLOR="Silver"]

  6. #5
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Byte Stream to String Conversion

    This would be a high speed modem converting bytes to strings. Faster transfer rates of bits per second. Over or through a cable tv company and better internet use.

Similar Threads

  1. Replies: 1
    Last Post: January 7th, 2014, 08:46 AM
  2. Replies: 1
    Last Post: April 7th, 2011, 01:32 PM
  3. RSA Decryption with Java.security - Hex to dec to byte array conversion...
    By SeanSeanston in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 15th, 2010, 09:48 AM
  4. UTF - 8 / Byte Stream
    By JavaCODER in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 16th, 2010, 02:26 PM
  5. [SOLVED] utf-16 byte[] to string conversion
    By Gerhardl in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 25th, 2010, 07:06 AM