How to convert/display a unicode string in vb.net

I spent hours trying to figure out what to do to be able to make my unicode string (which contained english characters) to show up in my web browser. It always showed up as Question Marks (?????) or just plain garbled text (like Chinese characters). I tried using different responseencoding and fileencoding values in web.config, but to no avail.

For some reason Microsoft has change StrConv function and has removed the vbUniCode and vbFromUnicode constants. So, you can no longer use that to get an ASCII representation of the string. Well, this is what finally worked for me (assume the string to be converted is in the variable strBody):

Imports System.Text

‘This will take care of unicode messages. Basically it strips everything down to ASCII

strBody = Encoding.ASCII.GetString(Encoding.Unicode.GetBytes(strBody))

Before :

encode1.JPG

After :

encode2.JPG

Advertisements

27 thoughts on “How to convert/display a unicode string in vb.net

  1. Definitely useful. I’ll bookmark this and try to remember where I saw it … I know I’ll need to know this at some point in the near future. (esp since I’ll be taking asp.net this semester)

    Thanks

  2. is it a bug?
    1) Dim str As String = “123456789”
    2) str = Encoding.Unicode.GetString(Encoding.ASCII.GetBytes(str))
    3)str = Encoding.ASCII.GetString(Encoding.Unicode.GetBytes(str))
    result is :
    12345678 !!!!
    where is 9?
    Please Help me!!

  3. Hello Sir,
    I want to display the Unicode text to Hindi.
    I had a text which contains the Unicode data i want to display that data to hindi format.
    Ex. Unicode data=0928;0940;0932;092E
    Hindi Data =निलम
    Please tell me how to convert Unicode data to Hindi using vb.net.

    Thanks in Advance,
    Atul Bari.

  4. i try to use it but does not working.
    i want to make a hindi registration form in which user will type english letter but it will be converted in hindi like gmail.

    Thanks

  5. Firstly you have to create Function in SQL (set data type of the field as nvarchar) which converts the Single Unicode to Hindi Character like (0905 = अ) and other is Procedure which convert Whole unicode word to Hindi word like (0905;0924;0941;0932 = अतुल)

    1. Hex Function As follows:

    use master
    go
    create function Hex (@hexnum varchar(4)) returns nvarchar
    as
    begin
    declare @decinum int
    ,@hindichar nvarchar
    ,@d4 int
    ,@d3 int
    ,@d2 int
    ,@d1 int

    select @d4 = case substring(@hexnum, len(@hexnum),1) when ‘A’ then 10
    when ‘B’ then 11
    when ‘C’ then 12
    when ‘D’ then 13
    when ‘E’ then 14
    when ‘F’ then 15
    else
    convert(int,substring(@hexnum, len(@hexnum),1))
    end
    select @d3 = case substring (@hexnum, len (@hexnum)-1,1) when ‘A’ then 10
    when ‘B’ then 11
    when ‘C’ then 12
    when ‘D’ then 13
    when ‘E’ then 14
    when ‘F’ then 15
    else
    convert(int, substring (@hexnum, len (@hexnum)-1,1))
    end
    select @d2 = case substring (@hexnum, len (@hexnum)-2,1) when ‘A’ then 10
    when ‘B’ then 11
    when ‘C’ then 12
    when ‘D’ then 13
    when ‘E’ then 14
    when ‘F’ then 15
    else
    convert(int, substring (@hexnum, len (@hexnum)-2,1))
    end
    select @d1 = case substring (@hexnum, len (@hexnum)-3,1) when ‘A’ then 10
    when ‘B’ then 11
    when ‘C’ then 12
    when ‘D’ then 13
    when ‘E’ then 14
    when ‘F’ then 15
    else
    convert(int, substring (@hexnum, len (@hexnum)-3,1))
    end

    select @decinum = 1*@d4 + 16*@d3 + 16*16*@d2 + 16*16*16*@d1

    select @hindichar = nchar(@decinum)
    return @hindichar
    end

    2. Procedure To convert all Unicode string to Hindi String (0905;0924;0941;0932 = अतुल)

    use Master
    go
    create procedure hinditextconverter @i_db2hexstr nvarchar(1024)
    as
    declare
    @pos int
    ,@len int
    ,@l nvarchar(256)
    ,@p nvarchar(256)
    ,@retstr nvarchar(1000)
    ,@uninum nvarchar(4)

    select
    @pos = 0
    ,@retstr = ”

    set @len = len(@i_db2hexstr)

    while (@pos @len)
    begin
    select @uninum = substring(@i_db2hexstr,@pos+1,4)
    select @pos = charindex(‘;’,@i_db2hexstr,@pos+1)
    exec @p = PRAN..hex @uninum
    select @retstr = @retstr + @p
    if @pos = 0
    break
    end
    select @retstr

    • I am trying ur code but in the second procedure hinditextconverter
      exec @p = PRAN..hex @uninum
      the PRAN is a separate module, which is not found so the above code is not working. I am in need of the above code.
      Please help me i want to convert hindi name into unicode character like
      कमला =915;092E;0932;093E^0915;0941;092E;093E;0930;0940

  6. You can assign the database field by which u r converting the unicode to devnagari font. (select some unicode enabled font like “Arial Unicode MS”)

    u can convert the Unicode to by using above solution & then u can show the data in text box or else.

    • Hi Aul,

      I tried your code but does not work, wud you be able to guide me? Cud you contact me via mail at mohan_chugani at yahoo dot com.

      Appreciate your help.

      Thanks.

  7. i am having data in hindi and i need to convert that file into english. can any body help on this

    thnx in advance
    sree

    • use the following code:-

      For Each c As Char In textbox.text
      hexValue = Hex(System.Convert.ToInt32(c))
      Next
      append 0’s if required. I searched a lot and then got it finally 🙂

  8. I want to convert ISFOC to unicode.Can anyone help me?

    i have text in dvbw-tt font and i want to convert it to unicode.Please help me

  9. hello
    i want to print hindi and eglish in both language in dos mode in vb.net someone help me?
    it urgent befor 31/03/2011

  10. I need your help in,
    I am working on a project, MYSQL and PHP based,
    with WAMP SERVER
    I have a field storytext, (data collected in URDU language UTF-8) detail are:

    Field:storytext
    Type:text
    Collation:utf8_unicode_ci

    Retrieving URDU Data from MY SQL 5.5.8 running on wamp server with PHP is OK,

    And the soft ware is only for Internal use not web base!
    ————————————————————————————————————————
    HERE IS THE PROBLEM:

    One section of it is designed on Visual Studio.NET 2010 in which

    I have a datagrid, on clicking any row I got the TEXT in details on text box,

    which show URDU TEXT, from data base here is the problem bind with DATA GRID and TEXT BOX.

    I got this……….

    DESP. ITEM

    پروگرام رانا مب
    شر ايٹ پرائم ٹائم ميں
    گ٠تگو کرتے Û ÙˆØ¦Û’ وزير٠اعظم Ú©Û’
    وکيل Ú†ÙˆÛ Ø¯Ø±ÙŠ اعتزاز احسن
    کا Ú©Û Ù†Ø§ تھا Ú©Û ØµØ¯Ø± Ú©Û’ Ø¹Û Ø¯Û’
    Ú©Ùˆ استشني حاصل Û Û’
    وزير٠اعظم Ø
    ————————————————————————————————————————
    IT SHOULD BE LIKE THIS:

    وزيرِ اعظم کے وکيل چوہدري اعتزاز احسن کا کہنا تھا کہ صدر کے عہدے کو استشني حاصل ہے وزيرِ اعظم جيل سے بھي وزارتِ عظمي چلاسکتے ہيں۔

    ————————————————————————————————————————

    I also use in visual studio

    cnString = “datasource=localhost;username=root;password=;database=xyz;charset=utf8;”

  11. With havin so much content and articles do you ever run into any problems of plagorism or copyright violation?
    My site has a lot of exclusive content I’ve either authored myself or outsourced but it looks like a lot of it is popping it up all over the internet without my permission. Do you know any solutions to help protect against content from being ripped off? I’d definitely appreciate
    it.

    • I just write all this stuff to share my experiences with the rest of the world, so if someone copies my content it really doesn’t bother me. I am not making any money off of it anyway.

      But you are right. Someone could run into problems if they have content which they need to protect. In fact, I did find a website which had started serving my blogs and was showing ads and earning money off of my content. Thankfully, Google flagged them and removed that from search results.

      Frankly, speaking I don’t know what someone would need to do if it happens though!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s