title: Decoding URL encoded UTF-8 strings in VBScript
body: <p>I need to URL decode a string in a VBScript. The string may contain Unicode characters which are encoded as multiple bytes as per UTF-8. So for example "Paris%20%E2%86%92%20Z%C3%BCrich" would decode to "Paris → Zürich".</p>
  
  <p>To do the job, I'm using a piece of code that looks like this:</p>
  
  <pre><code>Function URLDecode(str)
      set list = CreateObject("System.Collections.ArrayList")
      strLen = Len(str)
      for i = 1 to strLen
          sT = mid(str, i, 1)
          if sT = "%" then
              if i + 2 &lt;= strLen then
                  list.Add cbyte("&amp;H" &amp; mid(str, i + 1, 2))
                  i = i + 2
              end if
          else
              list.Add asc(sT)
          end if
      next
      depth = 0
      for each by in list.ToArray()
          if by and &amp;h80 then
              if (by and &amp;h40) = 0 then
                  if depth = 0 then Err.Raise 5
                  val = val * 2 ^ 6 + (by and &amp;h3f)
                  depth = depth - 1
                  if depth = 0 then
                      sR = sR &amp; chrw(val)
                      val = 0
                  end if
              elseif (by and &amp;h20) = 0 then
                  if depth &gt; 0 then Err.Raise 5
                  val = by and &amp;h1f
                  depth = 1
              elseif (by and &amp;h10) = 0 then
                  if depth &gt; 0 then Err.Raise 5
                  val = by and &amp;h0f
                  depth = 2
              else
                  Err.Raise 5
              end if
          else
              if depth &gt; 0 then Err.Raise 5
              sR = sR &amp; chrw(by)
          end if
      next
      if depth &gt; 0 then Err.Raise 5
      URLDecode = sR
  End Function
  </code></pre>
  
  <p>This seems to be working well, but it looks exaggeratedly complex to me. In times of HTML5 and web standards, there must be a simpler way to get this done without a bunch of hand-made loops and conditions. Any suggestions?</p>