2017-12-15

Navigation

Skip Navigation Links

Hash algorithms

Asymmetric Algorithms

Symmetric Cipher Algorithms

Encoding Algorithms

Compression Algorithms

Pseudo Random Number Algorithms

Steganography

Library Wrappers

String Comparison

Others

Syntax highlighting by Prism
PBCrypto.com Mirror

URLEncode

Algorithm creator(s)

n/a


PB author(s)

Florent Heyworth


Description

Useful when passing URL query strings which might need to be encoded to preserve them. Corresponds to the application/x-www-form-urlencoded media type.


Note

n/a


Source

https://forum.powerbasic.com/forum/user-to-user-discussions/source-code/23293-cgi-urlencode?t=22680


See also

n/a


Source Code

Download source code file urlencode.bas (Right-click -> "Save as ...")

FUNCTION UrlEncode( BYVAL sToEscape AS STRING ) AS STRING
   'formats a string to comply with CGI application/x-www-form-urlencoded
   'media type. Useful in CGI to be used as a query part or to pass
   'variables around
   REGISTER lIterIn AS LONG
   REGISTER lIterOut AS LONG
   LOCAL lLength AS LONG
   LOCAL pbByteIn AS BYTE PTR
   LOCAL pbByteOut AS BYTE PTR
   LOCAL pszReturnStr AS ASCIIZ PTR
   LOCAL sOut AS STRING
   LOCAL sHex AS STRING * 2
   lLength = LEN(sToEscape)
   'Allocate to len(sToEscape) * 3 + 1 (worst case scenario)
   sOut = SPACE$( (lLength * 3)+1 )
   pbByteIn = STRPTR( sToEscape )
   pbByteOut = STRPTR( sOut )
   DO UNTIL lIterIn = lLength
      SELECT CASE @pbByteIn[lIterIn]
             CASE 48 TO 57, 65 TO 90, 97 TO 122
                  @pbByteOut[lIterOut] = @pbByteIn[lIterIn]
             CASE 32
                  @pbByteOut[lIterOut] = 43 '+
             CASE ELSE
                  IF @pbByteIn[lIterIn] < 16 THEN
                     @pbByteOut[lIterOut] = 37 '%
                     @pbByteOut[lIterOut+1] = 48 '0
                     @pbByteOut[lIterOut+2] = ASC(HEX$(@pbByteIn[lIterIn]))
                  ELSE
                     @pbByteOut[lIterOut] = 37 '%
                     sHex = HEX$(@pbByteIn[lIterIn])
                     @pbByteOut[lIterOut+1] = ASC(sHex,1)
                     @pbByteOut[lIterOut+2] = ASC(sHex,2)
                  END IF
                  lIterOut = lIterOut + 2
      END SELECT
      INCR lIterIn
      INCR lIterOut
   LOOP
   @pbByteOut[lIterOut] = 0 'terminate the string
   pszReturnStr = STRPTR( sOut )
   FUNCTION = @pszReturnStr
END FUNCTION

Mirror provided by Knuth Konrad