Do you like this article? Please, rate it and write review!
Rated:
by Aspin.com users
| |
| | Top messages |
| 22.3.2003 19:18:41 | |
| 4.5.2002 9:16:43 | |
| 12.6.2003 9:14:29 | |
Convert data to another charset/codepage in vb net | Areas>ASP / ASP.Net>Functions>Conversion Areas>Languages>VB.Net | |
| 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")
|
See also CharSetConvert method of
ByteArray object for similar functionality in plain ASP or WSH scripts (in VB Script, JScript).
|
|
If you like this page, please include next link on your pages:
<A
Href="http://www.motobit.com/tips/detpg_convert-charset-vbnet/"
Title="Short sample to convert String Unicode
data to another charset/codepage using
System.Text.Encoding"
>Convert data to another charset/codepage in vb net</A>
|
|