Delphi: urldecode urlencode

Source: Internet
Author: User

Function urldecode (const S: string): string;
VaR
Idx: integer; // loops thru chars in string
HEX: string; // string of hex characters
Code: integer; // hex character code (-1 on error)
Begin
// Intialise result and string Index
Result: = '';
Idx: = 1;
// Loop thru string decoding each character
While idx <= length (s) do
Begin
Case s [idx]
'% ':
Begin
// % Shocould be followed by two hex digits-exception otherwise
If idx <= length (S)-2 then
Begin
// There are sufficient digits-try to decode hex digits
HEX: = s [idx + 1] + s [idx + 2];
Code: = sysutils. strtointdef ('$' + hex,-1 );
INC (idx, 2 );
End
Else
// Insufficient digits-Error
Code: =-1;
// Check for error and raise exception if found
If code =-1 then
Raise sysutils. econverterror. Create (
'Invalid hex digit in url'
);
// Decoded OK-add character to result
Result: = Result + CHR (CODE );
End;
'+ ':
// + Is decoded as a space
Result: = Result +''
Else
// All other characters pass thru unchanged
Result: = Result + s [idx];
End;
INC (idx );
End;
End;

function urlencode (const S: string; const inquerystring: Boolean): string;
var
idx: integer; // loops thru characters in string
begin
result: = '';
for idx: = 1 to length (s) DO
begin
case s [idx] of
'A '.. 'Z', 'A '.. 'Z', '0 '.. '9 ','-','_','. ':
result: = Result + s [idx];
'':
If inquerystring then
result: = Result + '
else
result: = Result +' % 20';
else
result: = Result + '%' + sysutils. inttohex (ord (s [idx]), 2);
end;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.