Kerala Microsoft Users Group
Pay $0.00 for Windows Server 2008 and SQL Server 2008 licenses.

Convert UTF-16 into character in .net

Latest post 06-26-2009 7:55 PM by Shajan. 5 replies.
  • 06-25-2009 6:30 AM

    Convert UTF-16 into character in .net

    Hi all,

    In my application, Some data's are there in DataBase as UTF-16 unicode format.I just wanted to convert those chracters into normal character and display that in screen. Can anyone help me out to convert this UTF16 format to normal characters??

    Thanks

    Jegan

    • Post Points: 45
  • 06-25-2009 1:02 PM In reply to

    Re: Convert UTF-16 into character in .net

    Try this

    Encoding.Convert(Encoding.BigEndianUnicode, Encoding.ASCII, Encoding.UTF8.GetBytes("Text2Convert"))

    Thanks

    Anuraj P
    http://www.dotnetthoughts.net

    THIS POSTING IS PROVIDED "AS IS" WITH NO WARRANTIES, AND CONFERS NO RIGHTS.
    BEWARE OF BUGS IN THE ABOVE CODE; I HAVE ONLY PROVED IT CORRECT, NOT TRIED IT.

    • Post Points: 5
  • 06-26-2009 1:21 AM In reply to

    Re: Convert UTF-16 into character in .net

    Hi  amjrgs,

    FYI, from msdn

    A string is an object of type String whose value is text. Internally, the text is stored as a readonly collection of Char objects, each of which represents one Unicode character encoded in UTF-16. There is no null-terminating character at the end of a C# string (unlike C and C++); therefore a C# string can contain any number of embedded null characters ('\0'). The length of a string represents the number of characters regardless of whether the characters are formed from Unicode surrogate pairs or not. To access the individual Unicode code points in a string, use the StringInfo object.

    Am assuming in database you are storing the UTF-16 string as bytes.

    You can read the byte array from database and convert it to string as shown below.

    byte[] bytes = ///Read from database
    Console.WriteLine(Encoding.Unicode.GetString(bytes));

    • Post Points: 25
  • 06-26-2009 5:48 AM In reply to

    Re: Convert UTF-16 into character in .net

    I have tried that way but i have got the same string what i am getting from DB.

    Actually in my company we have two applications.One in J2ME and one in .Net. Input are coming into mobile as different language and they are converting that into UTF-16 format and store in database. So we(.Net) just wanted to decode and dispaly that in web page.

    I have tried in the above mentioned code but it still give the same input string back.

    Thanks

    • Post Points: 45
  • 06-26-2009 6:42 AM In reply to

    Re: Convert UTF-16 into character in .net

    Did you tried both codes?

    Thanks

    Anuraj P
    http://www.dotnetthoughts.net

    THIS POSTING IS PROVIDED "AS IS" WITH NO WARRANTIES, AND CONFERS NO RIGHTS.
    BEWARE OF BUGS IN THE ABOVE CODE; I HAVE ONLY PROVED IT CORRECT, NOT TRIED IT.

    • Post Points: 5
  • 06-26-2009 7:55 PM In reply to

    Re: Convert UTF-16 into character in .net

    If you have a few sample DB strings in a byte array, can you print out each byte in hex for the sample strings and reply. If you already know what the strings are (using J2ME), also copy them in the reply.

    If you have a choice for the test string, use 'ABC', 'DEF' (consecutive byte codes that is easy to spot)

    -Shajan

    • Post Points: 5
Page 1 of 1 (6 items) | RSS
Pay $0.00 for Windows Server 2008 and SQL Server 2008 licenses.