ASP Shopping Cart
,
Forum
&
BBS
- all for $20 from
CodeToad Plus!
Home
||
ASP
|
ASP.Net
|
C++/C#
|
DHTML
|
HTML
|
Java
|
Javascript
|
Perl
|
VB
|
XML
||
CodeToad Plus!
||
Forums
||
RAM
Search Site:
Home
»
ASP
» Article
Decode and Encode UTF-8
Article by:
Hans Kalle (9/2/2006)
Add to Favourites
Tell a friend
Summary:
When reading and writing xml-files like rss-feeds, one needs to convert data from the Windows character set to UTF-8. This script is all you need.
Viewed:
159727
times
Rating (34 votes):
3.9
out of 5
Rate this Article
Read Comments
Post Comments
Decode and Encode UTF-8
All encoding and decoding is done by the functions in the utf8.asp file:
Select All Code
<% option explicit ' Simple functions to convert the first 256 characters ' of the Windows character set from and to UTF-8. ' Written by Hans Kalle for Fisz ' http://www.fisz.nl 'IsValidUTF8 ' Tells if the string is valid UTF-8 encoded 'Returns: ' true (valid UTF-8) ' false (invalid UTF-8 or not UTF-8 encoded string) function IsValidUTF8(s) dim i dim c dim n IsValidUTF8 = false i = 1 do while i <= len(s) c = asc(mid(s,i,1)) if c and &H80 then n = 1 do while i + n < len(s) if (asc(mid(s,i+n,1)) and &HC0) <> &H80 then exit do end if n = n + 1 loop select case n case 1 exit function case 2 if (c and &HE0) <> &HC0 then exit function end if case 3 if (c and &HF0) <> &HE0 then exit function end if case 4 if (c and &HF8) <> &HF0 then exit function end if case else exit function end select i = i + n else i = i + 1 end if loop IsValidUTF8 = true end function 'DecodeUTF8 ' Decodes a UTF-8 string to the Windows character set ' Non-convertable characters are replace by an upside ' down question mark. 'Returns: ' A Windows string function DecodeUTF8(s) dim i dim c dim n i = 1 do while i <= len(s) c = asc(mid(s,i,1)) if c and &H80 then n = 1 do while i + n < len(s) if (asc(mid(s,i+n,1)) and &HC0) <> &H80 then exit do end if n = n + 1 loop if n = 2 and ((c and &HE0) = &HC0) then c = asc(mid(s,i+1,1)) + &H40 * (c and &H01) else c = 191 end if s = left(s,i-1) + chr(c) + mid(s,i+n) end if i = i + 1 loop DecodeUTF8 = s end function 'EncodeUTF8 ' Encodes a Windows string in UTF-8 'Returns: ' A UTF-8 encoded string function EncodeUTF8(s) dim i dim c i = 1 do while i <= len(s) c = asc(mid(s,i,1)) if c >= &H80 then s = left(s,i-1) + chr(&HC2 + ((c and &H40) / &H40)) + chr(c and &HBF) + mid(s,i+1) i = i + 1 end if i = i + 1 loop EncodeUTF8 = s end function %>
To test the functions, one can use the code below. Place this code in a separate file, utf8test.asp for example.
Select All Code
<!--#include file="utf8.asp"--> <html> <head> <title>Test UTF encoding en decoding</title> <head/> <body> <h1>Decoding</h1> <p>Kopiėren ok: <%=DecodeUTF8("KopiÅ«ren")%></p> <p>Kopiėren error: <%=DecodeUTF8("Kopiėren")%></p> <p>Kopiėren error: <%=DecodeUTF8("KopiÅren")%></p> <p>Kopiėren error: <%=DecodeUTF8("KopiÅ««ren")%></p> <p>Kopiėren error: <%=DecodeUTF8("Kopią«ren")%></p> <h1>Encoding</h1> <p>Kopiėren UTF-8: <%=EncodeUTF8("Kopiėren")%></p> <p>Kopiėren forth & back: <%=DecodeUTF8(EncodeUTF8("Kopiėren"))%></p> <h1>Testing encoding</h1> <p>Kopiėren: <%=IsValidUTF8("Kopiėren")%></p> <p>KopiÅ«ren: <%=IsValidUTF8("KopiÅ«ren")%></p> <p>KopiÅ««ren: <%=IsValidUTF8("KopiÅ««ren")%></p> <p>Aeroplane: <%=IsValidUTF8("Aeroplane")%></p> </body> </html>
Good luck and have fun!
Useful Links
Online Fax Reviews
Advertise here
CodeToad Experts
Can't find the answer?
Our
Site experts
are answering questions for free in the
CodeToad forums
Rate this article:
Poor
Excellent
View highlighted Comments
User Comments on 'Decode and Encode UTF-8'
RELATED ARTICLES
ASP FilesystemObject
by
Jeff Anderson
An introduction to the Filesystemobject
ASP GetTempName
by
Jeff Anderson
Use the GetTempName method to create a randomly generated temporary file on the server.
ASP Format Date and Time Script
by
Jeff Anderson
An ASP script showing the variety of date and time formats possible using the FormatDateTime Function.
ASP OpenTextFile
by
Jeff Anderson
An introduction to the OpenTextFile Method of the FileSystemObject
Email validation using Regular Expression
by
Jeff Anderson
Using regular expression syntax is an exellent way to thoroughly validate an email. It's possible in ASP.
Add or Subtract Hours in SQL or ASP using DateAdd
by
Jeff Anderson
A beginners guide to using the SQL DATEADD function to add or subtract hours. Particularly useful when setting the time displayed on the ASP page to a different time zone (eg when the server is in the US, and the site is for a UK audience).
The asp:radiobutton and asp:radiobuttonlist control
by
David Sussman, et al
In HTML, radio buttons are used when we need to make multiple sets of choices available, but we want the user to select only one of them.
ASP FileExists
by
Jeff Anderson
An introduction to the FileExistsMethod of the FileSystemObject
The asp:checkbox and asp:checkboxlist control
by
David Sussman, et al
Checkboxes are similar to radio buttons, and in HTML, they were used to allow multiple choices from a group of buttons.
Concatenate strings in sql
by
Jeff Anderson
A brief introduction to concatenating strings in an sql query (using SQL server or access databases).
Recent Forum Threads
deleting data from databse to dropdown list
matrix addition
Re: Storing data from HTML to Excel or TXT
Re: function within loop problem
Re: Ō“ņ„Æ„é„Ö¤Ļ „Ę©`„é©`„ᄤ„É£ņ£±£±„¢„¤„¢„ó ¤Ī£··¬ ¤Ē¤¹
Re: Replace
Re: ćæć¤ććŖć¹ć AP2ć¢ć¤ć¢ć³ 712ć®ę å �
Re: SMS from Perl using HTTP request
Re: Charl Schwartzel
Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text
privacy
-
contact
-
advertise
-
hot links
-
link to us
-
submit your article
© Copyright codetoad.com 2001-2013