• home
  • forum
  • my
  • kt
  • download
  • Charset convert

    Author: 2007-07-04 15:01:49 From:

    Sometimes you will need to export some data from an ASP page and use another charset than the encoding you have specified in Web.config. If you have next line in web.config, all string response data from Response.Write method are converted to utf-8. <globalization requestEncoding="utf-8" responseEncoding="utf-8" />

        You can simply change response charset and content-type with next commands:

      Response.Charset = "windows-1250"
      Response.ContentType = "text/plain" 

        But if you try to write a string data using Response.Write method, the result will confuse client side - the response data will be encoded as utf-8, but response header will have charset=windows-1250. So you have to convert the unicode data to data with windows-1250 code page. I created short function to convert such data from Unicode (VB String) to another code page (for example windows-1250). I created a simple conversion function using System.Text.Encoding:

    Function EncodeString(ByRef SourceData As String, ByRef CharSet As String) As Byte()
      'get a byte pointer To the source data
      Dim bSourceData As Byte() = System.Text.Encoding.Unicode.GetBytes(SourceData)
    
      'get destination encoding 
      Dim OutEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding(CharSet)
    
      'Encode the data To destination code page/charset
      Return System.Text.Encoding.Convert(System.Text.Encoding.Unicode, OutEncoding, bSourceData)
    End Function
    

    Then you can simply write:

    Dim Data As string
    
    ' Fill the string variable with some data
    Data = "Some string with any chars suitable For windows-1250 charset"
    
    'Set content-type
    Response.ContentType = "text/plain"
    'Set out charset
    Response.Charset = "windows-1250"
    'Write string converted To the charset.
    Response.BinaryWrite EncodeString(Data, "windows-1250")

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      NET (110)

    New

    Hot