flogiston

Blog

Classic ASP - UTF-8 to Cookies

· admin · Classic asp

I spend quite some time today trying to figure out how to get UTF-8 encoded text into a cookie. It always ended up as a bunch of questionmarks. I even tried urlencoding/urldecoding the values but alas.

But now I found the way. The trick is to convert the UTF-8 string to ANSI before writing it to a cookie and to convert it back again when reading from the cookie. I first tried converting to us-ascii or iso 8859-1, but they didn't work, really. I found snippets from various websites (always the same kind of routine). Here's the sample code:
[vb]
<%@ Codepage=65001%>
<%
Response.Charset="utf-8"
Session.Codepage=65001
' convert a text in a given encoding to a binary stream
Function StringToBinary(Text, CharSet)
Const adTypeText = 2
Const adTypeBinary = 1
'Create Stream object
Dim objStream 'As New Stream
Set objStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save text/string data.
objStream.Type = adTypeText
'Specify charset For the source text (unicode) data.
If Len(CharSet) > 0 Then
objStream.CharSet = CharSet
Else
Err.Raise vbObjectError + 1, "IllegalArgument", "Charset is required"
End If
'Open the stream And write text/string data To the object
objStream.Open
objStream.WriteText Text
'Change stream type To binary
objStream.Position = 0
objStream.Type = adTypeBinary
'Ignore first two bytes - sign of
objStream.Position = 0
'Open the stream And get binary data from the object
StringToBinary = objStream.Read
End Function
' convert a binary stream to text in a given encoding
Function BinaryToString(binData, CharSet)
Dim objStream
Dim strTmp
Const adTypeBinary = 1
Const adTypeText = 2
Const adModeReadWrite = 3
' init stream
Set objStream = server.CreateObject("ADODB.Stream")
If CharSet<>"" Then
objStream.CharSet = CharSet
Else
Err.Raise vbObjectError + 1, "IllegalArgument", "Charset is required"
End If
objStream.Mode = adModeReadWrite
objStream.Type = adTypeBinary
objStream.Open
' write bytes into stream
objStream.Write binData
objStream.Flush
' rewind stream and read text
objStream.Position = 0
objStream.Type = adTypeText
strTmp = objStream.ReadText
' close up and return
objStream.Close
BinaryToString = strTmp
End Function

' Function to bind the two: convert orig charset to a binary stream and then back again to the new charset
Function convertCharSet(text, origCharset, newCharSet)
Dim binaryData
binaryData = StringToBinary(text, origCharset)
convertCharSet = BinaryToString(binaryData, newCharSet)
End Function

%>
<html>
<head>
<title>UTF-8 Cookie Test<title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<%

Dim utf8String
Dim asciiString
source: http://www.tek-tips.com/viewthread.cfm?qid=1392917
utf8String = "Реєстраційна форма Клубу VAIO"
asciiString = convertCharSet(utf8String, "utf-8", "X-ANSI") '"ISO-8859-1")
Response.Cookies("testutf8")("utf8String")= asciiString
'response.write utf8String & "<br/>"

response.write "in memory: " & utf8String & "<br/>"
response.write "asciiString:" & asciiString & "<br/>"
response.write "asciiStringConverted:" & convertCharSet(asciiString, "X-ANSI", "utf-8") & "<br/>"
response.write "in cookie: " & convertCharSet(Request.Cookies("testutf8")("utf8String"), "X-ANSI", "utf-8") & "<br/>"
>

</body>

</html>
[/vb]