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 6 of 6

Thread: utf-16 byte[] to string conversion

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default utf-16 byte[] to string conversion

    Hello All,

    I have been searching the internet for help with a problem..
    I have a file that gets sent from a desktop pc to my java mobile device over gprs,
    and I need to parse that file to reflect the contents on the screen.
    Somehow I am unable to convert the data successfully to unicode representation.

           byte[] data1 = {0x4E,0x61,0x76, 0x20, 0x54,(byte) 0xEA, 0x73,0x74};
     
        	String[] encoders = {"US-ASCII","ISO-8859-1","UTF-8","UTF-16BE","UTF-16LE","UTF-16"};
        	int encId = 0;
     
        	while (encId < encoders.length)
        	{
        		try
    			{
        			System.out.println("Encoding: " + encoders[encId]);
    				String s1 = new String(data1,0,data1.length, encoders[encId]);
    				System.out.println("Name: " + s1);
    				encId ++;
    			}
    			catch (UnsupportedEncodingException e)
    			{
    				e.printStackTrace();
    			}
        	}

    my output for that code:
    Encoding: US-ASCII
    Name: Nav T?st
    Encoding: ISO-8859-1
    Name: Nav T�st
    Encoding: UTF-8
    Name: Nav T
    Encoding: UTF-16BE
    Name: ????
    Encoding: UTF-16LE
    Name: ????
    Encoding: UTF-16
    Name:

    what I need... "Nav Têst" ( I know that's not how you spell "test")

    I have also tried the DataInputStream class, but with no luck. Any other ideas?


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: utf-16 byte[] to string conversion

    Interestingly here are my results,
    Encoding: US-ASCII
    Name: Nav T?st
    Encoding: ISO-8859-1
    Name: Nav Têst
    Encoding: UTF-8
    Name: Nav T?st
    Encoding: UTF-16BE
    Name: ????
    Encoding: UTF-16LE
    Name: ????
    Encoding: UTF-16
    Name: ????

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: utf-16 byte[] to string conversion

    Indeed interesting...

    So that means that the ISO encoder is the correct one to use.
    Will the results be affected by the java runtime? As far as I know, this is 1.4 or lower.

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: utf-16 byte[] to string conversion

    My results are from jre 1.6

    So perhaps they changed the implementation

  5. The Following User Says Thank You to Freaky Chris For This Useful Post:

    Gerhardl (February 25th, 2010)

  6. #5
    Junior Member
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: utf-16 byte[] to string conversion

    Quote Originally Posted by Freaky Chris View Post
    My results are from jre 1.6

    So perhaps they changed the implementation
    Thanks Chris.. Seems that is the console window that cannot display unicode,
    on the terminal it is fine... I was only looking at the console output for desired results. My Bad.

  7. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: utf-16 byte[] to string conversion

    That would also work. Well if that is solved then please mrk it as so

Similar Threads

  1. How Convert a Byte array to a image format
    By perlWhite in forum Algorithms & Recursion
    Replies: 7
    Last Post: February 19th, 2011, 03:16 PM
  2. Explicit Conversion?
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 11th, 2009, 11:40 PM
  3. binary conversion..
    By chronoz13 in forum Java Theory & Questions
    Replies: 8
    Last Post: September 16th, 2009, 10:47 AM
  4. byte[] from vector
    By perlWhite in forum Collections and Generics
    Replies: 1
    Last Post: August 26th, 2009, 05:10 AM
  5. Convert Vector to Byte Array
    By perlWhite in forum Java Theory & Questions
    Replies: 0
    Last Post: August 25th, 2009, 05:45 AM