Classic ASP TextStream and unicode

I had the following error from a piece of code which had been working:

Microsoft VBScript runtime error '800a0005'
Invalid procedure call or argument

A snippet of what was throwing this:

set os = fs.createtextfile(fn, true)
os.write textmsg
os.close

The error was on the os.write call. The error I eventually realised was caused by form input, the value that had been output to the browser contained ‎ i.e.:

<input type="text" name="val" value="&#8206;">

Which on submission was encoded as a char whose ascii value was 1. The TextStream  Write method is only intended to handle ascii out. Simple solution write a bit of code to strip all non printable characters from the input.

  function NonPrintStrip(s)
    dim r, i
    for i = 1 to len(s)
      if asc(mid(s, i, 1)) >= 32 and asc(mid(s, i, 1)) < 128 then
        r = r & mid(s, i, 1)
      end if
    next
    NonPrintStrip = r
  end function

Tags: ,

Leave a Reply

 
Trojan Archer