[thelist] Encoding Tip

VOLKAN ÖZÇELİK volkan.ozcelik at gmail.com
Fri Sep 16 09:52:02 CDT 2005


<tip author="Volkan Ozcelik" type="Database and Encoding .Net">

This is a recent scenario I experienced with my hosting provider.
Posting it as a tip so that it may help others:

Suppose you have X encoded (say windows-1254) data in your database,
your DataReader returns Y-encoded (say windows-1252) Strings
and Your RequestEncoding iz Z (say windows-1254 again)

To be able to read and display the data without any loss you need to go through
several conversions: code in vb.net

'the data reader returns a byte array in windows-1252(iso-8859-1) format
'however the original text comes from the db iso-8859-9 (windows-1254) encoded .

Dim result as string=Dreader("txtText") 'read field txtText from the data reader

'windows-1254: Turkish extended latin.
Dim myEncoding As System.Text.Encoding= System.Text.Encoding.GetEncoding(1254)

'windows-1252: Latin English.
Dim myEncoding1252 As System.Text.Encoding=
System.Text.Encoding.GetEncoding(1252)

'The result that comes from the db has been incorrectly encoded with 
'windows-1252 (it should have the same encoding as the text in the database 
'which is windows-1254)
'
'So Convert the 1252-formatted String back to its original byte array.
Dim myByteArray As Byte()= myEncoding1252.getBytes(result)

'The page's response encoding was windows-1254 (iso-8859-9)
'So the bytes must be transformed back into a windows-1254 String as well.
response.write(myEncoding.getString(myByteArray))

'And all the characters lived together happily ever after.
</tip>

Cheers,
Volkan.


More information about the thelist mailing list