URL encoding: Difference between revisions

From Rosetta Code
Content added Content deleted
m (tidy up list)
m (Rm extra headings, URL and ASCII are acronyms)
Line 1: Line 1:
{{draft task}}
{{draft task}}
The task is to provide a function or mechanism to convert a provided string into URL encoding representation.

The task is to provide a function or mechanism to convert a provided string into url encoding representation.

== What is URL encoding? ==


In URL encoding, special characters, control characters and extended characters are converted into a percent symbol followed by a two digit hexadecimal code, So a space character encodes into %20 within the string.
In URL encoding, special characters, control characters and extended characters are converted into a percent symbol followed by a two digit hexadecimal code, So a space character encodes into %20 within the string.
Line 9: Line 6:
The following characters require conversion:
The following characters require conversion:


*Ascii control codes (Character ranges 00-1F hex (0-31 decimal) and 7F (127 decimal).
*ASCII control codes (Character ranges 00-1F hex (0-31 decimal) and 7F (127 decimal).
*Ascii symbols (Character ranges 32-47 decimal (20-2F hex))
*ASCII symbols (Character ranges 32-47 decimal (20-2F hex))
*Ascii symbols (Character ranges 58-64 decimal (3A-40 hex))
*ASCII symbols (Character ranges 58-64 decimal (3A-40 hex))
*Ascii symbols (Character ranges 91-96 decimal (5B-60 hex))
*ASCII symbols (Character ranges 91-96 decimal (5B-60 hex))
*Ascii symbols (Character ranges 123-126 decimal (7B-7E hex))
*ASCII symbols (Character ranges 123-126 decimal (7B-7E hex))
*Extended characters with character codes of 128 decimal (80 hex) and above.
*Extended characters with character codes of 128 decimal (80 hex) and above.


=== Example ===
'''Example'''


The string "<code><nowiki>http://foo bar/</nowiki></code>" would be encoded as "<code><nowiki>http%3A%2F%2Ffoo%20bar%2F</nowiki></code>".
The string "<code><nowiki>http://foo bar/</nowiki></code>" would be encoded as "<code><nowiki>http%3A%2F%2Ffoo%20bar%2F</nowiki></code>".


=== See also ===
'''See also'''


[[URL decoding]]
[[URL decoding]]

Revision as of 15:29, 17 June 2011

URL encoding is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The task is to provide a function or mechanism to convert a provided string into URL encoding representation.

In URL encoding, special characters, control characters and extended characters are converted into a percent symbol followed by a two digit hexadecimal code, So a space character encodes into %20 within the string.

The following characters require conversion:

  • ASCII control codes (Character ranges 00-1F hex (0-31 decimal) and 7F (127 decimal).
  • ASCII symbols (Character ranges 32-47 decimal (20-2F hex))
  • ASCII symbols (Character ranges 58-64 decimal (3A-40 hex))
  • ASCII symbols (Character ranges 91-96 decimal (5B-60 hex))
  • ASCII symbols (Character ranges 123-126 decimal (7B-7E hex))
  • Extended characters with character codes of 128 decimal (80 hex) and above.

Example

The string "http://foo bar/" would be encoded as "http%3A%2F%2Ffoo%20bar%2F".

See also

URL decoding

Perl

Use standard CGI module: <lang perl>use 5.10.0; use CGI;

my $s = 'http://foo/bar/'; say $s = CGI::escape($s); say $s = CGI::unescape($s);</lang>