Convert symbol to String (%F0%9F%8C%A0)

Asked

Viewed 309 times

15

The symbol is equal to %F0%9F%8C%A0, but how can I convert it into Delphi?

I tried several Urlencoders, but none of them returned the correct result.

I’m trying this way:

 for i := 1 to length(s) do
    result:= result+IntToHex(ord(s[i]),2);

But the result is D83CDF20

  • The conversion depends on a number of things, it depends on where you are getting the data. Basically just take each of the bytes that make up the character, and convert to hexadecimal.

  • and what were the results you have achieved so far?

  • See an example in PHP http://ideone.com/t6n2HA

  • hi more or less that I tried @Bacco for i := 1 to length(s) of result:= result+Inttohex(Ord(s[i]),2); but my result is different that is my D83CDF20 result

  • @CP8M Each encoding guards the character in a way, when you say "The symbol is equal to %F0%9F%8C%A0" it is not always true. It depends on the encoding. The result you probably want is UTF-8, but Delphi should save in another format.

  • I don’t know if it’s my computer, but is it that symbol? http://i.imgur.com/Lfwnlf9.png

Show 1 more comment

1 answer

13


I found your problem in the Soen, to Indy TIdURI cannot do this in current versions of Delphi without having access to its Unit (that no longer accompanies it). So there is a function capable of doing this, actually several!

Let’s use a method and see if we get to what you intend!

function frmTeste.EncodeURIComponent(const ASrc: string): UTF8String;
const
  HexMap: UTF8String = '0123456789ABCDEF';

  function IsSafeChar(ch: Integer): Boolean;
  begin
    if (ch >= 48) and (ch <= 57) then Result := True    // 0-9
    else if (ch >= 65) and (ch <= 90) then Result := True  // A-Z
    else if (ch >= 97) and (ch <= 122) then Result := True  // a-z
    else if (ch = 33) then Result := True // !
    else if (ch >= 39) and (ch <= 42) then Result := True // '()*
    else if (ch >= 45) and (ch <= 46) then Result := True // -.
    else if (ch = 95) then Result := True // _
    else if (ch = 126) then Result := True // ~
    else Result := False;
  end;
var
  I, J: Integer;
  ASrcUTF8: UTF8String;
begin
  Result := '';

  ASrcUTF8 := UTF8Encode(ASrc);

  I := 1; J := 1;
  SetLength(Result, Length(ASrcUTF8) * 3);

  while I <= Length(ASrcUTF8) do
  begin
    if IsSafeChar(Ord(ASrcUTF8[I])) then
    begin
      Result[J] := ASrcUTF8[I];
      Inc(J);
    end
    else if ASrcUTF8[I] = ' ' then
    begin
      Result[J] := '+';
      Inc(J);
    end
    else
    begin
      Result[J] := '%';
      Result[J+1] := HexMap[(Ord(ASrcUTF8[I]) shr 4) + 1];
      Result[J+2] := HexMap[(Ord(ASrcUTF8[I]) and 15) + 1];
      Inc(J,3);
    end;
    Inc(I);
  end;

  SetLength(Result, J-1);
end;

To use the function do this:

edtFonteDeDados.Text := EncodeURIComponent('');

Do the tests, await the Feedback!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.