Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus/Delphi - Decode Unicode escape sequences in a string

1 Posts
1 Users
0 Reactions
3,261 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

This function strips and convert (simple) unicode escape sequences from a string.
With those I mean sequences like "u0026" (which is an ampersand).

For example, one of the return strings in the JSON data of a Googleapis call results in:

u003cbu003eLois u0026amp; Clarku003c/bu003e: The New Adventures of Superman

But we'd like to read:

<bu>Lois &amp; Clark</b>: The New Adventures of Superman

This is the simple function I have used - keep in mind that it assumes that the first byte is useless, so in the number sequence "u0026" it will only look at the "26" part (the 2nd byte) which is the hex value of 38 (dec), which in ASCII is an ampersand (see Character Table).

function DecodeUnicodeEscape(const AStr: string): string;
var Nr:integer;
    NewStr, OldStr:string;
begin
  OldStr:=AStr;
  NewStr:='';
  while pos('u',OldStr)>0 do
    begin
      try
        Nr:=Hex2Dec(Copy(OldStr, pos('u',OldStr)+2, 4));
        NewStr:=NewStr+Copy(OldStr, 0, pos('u',OldStr)-1)+chr(Nr);
        oldStr:=Copy(OldStr, pos('u',OldStr)+6, Length(OldStr));
      except
        // do nothing and be quiet
      end;
    end;
  Result:=NewStr+OldStr;
end;                 

   
ReplyQuote
Share: