Saturday, February 25, 2017

#1 2005-10-21 08:09:32 am

From: Malasaña, Madrid
Registered: 2002-11-20
Posts: 2014

How can I url-encode a text in UTF-8 notation?

Some times you may need convert text to utf-8 notation for web transmission, so you can pass "c%C3%A1spita" instead of "cáspita" (eg, if you are filling a form using curl).

These are the steps:
-Convert the input text to utf-8 (and grab its ASCII representation).
-Convert every unsafe byte to its hexadecimal equivalent.

This is only a method, but you can ellaborate yours one:


tell app "Smile" to escapeURL "cáspita --> "c%C3%A1spita"

Well, well... I'll write some more code for the vanilla fans...


encodeTextUTF8("cáspita") --> "c%C3%A1spita"

to encodeTextUTF8(k)
   set someSafeChars to "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_."
   set f to {}
   repeat with i from 1 to (count k)
       set c to character i of k
       if c is not in someSafeChars then
           set tmp to text2utf8(c) --> transform to plain-ascii version of utf8
           set c to "%" & getHex(tmp's item 1)
           try --> may be two chars or not
               set c to c & "%" & getHex(tmp's item 2)
           end try
       end if
       set f's end to c
   end repeat
   return f as text
end encodeTextUTF8

to text2utf8(t)
   set theFile to "/tmp/utf8stuff" as POSIX file
   set f to (open for access theFile with write permission)
   set eof of f to 0
   write t to f as «class utf8»
   close access f
   read theFile
end text2utf8

to getHex(this_string)
   this_string as C string
       result * 512
   on error msg
       return text from character 22 of msg to character ((offset of "00» into type number" in msg) - 1) of msg
   end try
end getHex



Board footer

Powered by FluxBB

[ Generated in 0.037 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)