Topic: Some questions...

i need help figuring out howto get this javascript code made into delphi...



<script type="text/javascript">
<!--
function tohex() {
    var outValue = '';
    inValue= document.getElementById('pad').value;
        for (i=0; i<inValue.length; i++)
            outValue += '%' + inValue.charCodeAt(i).toString(16);
    document.getElementById('pad').value = outValue;
    }
function toascii() {
    var outValue = '';
    inValue= document.getElementById('pad').value;
        outValue = unescape(inValue);
    document.getElementById('pad').value = outValue;
    }   
//-->
</script>
<form method="post">
<textarea cols="76" rows="15" name="pad" id="pad"></textarea><br>
<input value="Translate into hex" onclick="if(document.getElementById){tohex();return false}" type="submit">
<input value="Translate to ASCII" onclick="if(document.getElementById){toascii();return false}" type="submit">
</form>

2

Re: Some questions...

Something like this (not tested):

function HexToInt(Hex: string): integer;
var
  n: integer;
begin
  result:=0;
  // if more than 8 hex digits, the first ones must be 0
  if length(Hex)>8 then
  begin
    for n:=1 to length(Hex)-8 do
    begin
      if Hex[n]<>'0' then
        raise EConvertError.CreateFmt('Hexadecimal value 0x%s out of range',[Hex]);
    end;
  end;
  for n:=1 to length(Hex) do
  begin
    result:=result shl 4;
    case UpCase(Hex[n]) of
      '0' : begin end;
      '1'..'9' : Inc(result,ord(Hex[n])-48); // 1 - 9
      'A'..'F' : Inc(result,ord(Hex[n])-55); // 10 - 15
    else
      raise EConvertError.CreateFmt('Invalid hexadecimal digit: %s',[Hex[n]]);
    end;
  end;
end;

// IntToHex is built in (include SysUtils)

function toHex(s: string): string;
var
  n: integer;
begin
  for n:=1 to length(s) do
  begin
    result:=result+'%'+IntToHex(ord(s[n]),2);
  end;
end;

function toAscii(s: string): string;
var
  n: integer;
begin
  result := '';
  n := 1;
  while n <= length(s) do
  begin
    if s[n]='%' then
    begin
      result:=result+chr(HexToInt(copy(s,n+1,2)));
      Inc(n,2);
    end
    else
      result:=result+s[n];
    Inc(n);
  end;
end;

Obviously, you need to get the values from somewhere (say, a TEdit). The HexToInt function is general purpose - it works for all 32 bit hex values. The toAscii function too - it will allow parts to be unencoded (say, "hel%6Co" = "hello").

toHex converts ALL characters to hex - just like the javascript function - it doesn't care if they could more efficiently be simply plain ascii characters. No idea why the function doesn't use escape(inValue) (or URIescape(inValue)), but I just stuck with what the original function does , since I don't know what you're going to use it for wink. For URL encoding,  it should be tested if the characters are legal, before encoding them. Something like:

function toHex(s: string): string;
var
  n: integer;
begin
  for n:=1 to length(s) do
  begin
    if not (s[n] in ['a'..'z','A'..'Z','0'..'9','-','_','.','!','*',#39,'(',')']) then
      result:=result+'%'+IntToHex(ord(s[n]),2)
    else
      result:=result+s[n];
  end;
end;

You may want to remove all the special characters ('-','_' etc.) from the list above. They're allowed (but not certain to be "safe") in URLs. Also remember that this function should not be used to encode the entire URL with host and path - just for encoding values to be sent via HTTP GET wink But again, don't know if that's what you want it for.