Skip to content

Instantly share code, notes, and snippets.

@freeonterminate
Last active January 4, 2016 21:19
Show Gist options
  • Save freeonterminate/8680239 to your computer and use it in GitHub Desktop.
Save freeonterminate/8680239 to your computer and use it in GitHub Desktop.
function ToCharCode(const iText: String; const iUTF8: Boolean): String;
var
M: TMarshaller;
function ToHexString(const iByte: TPtrWrapper): String;
var
Chars: PByteArray;
i: Integer;
SB: TStringBuilder;
begin
Result := '';
Chars := iByte.ToPointer;
SB := TStringBuilder.Create;
try
i := 0;
while (Chars[i] <> 0) do begin
SB.Append(' ');
SB.Append(IntToHex(Chars[i], 2));
Inc(i);
end;
Result := SB.ToString;
if (Result.StartsWith(' ')) then
Result := Result.Substring(1);
finally
SB.Free;
end;
end;
begin
if (iUTF8) then
Result := ToHexString(M.AsUtf8(iText))
else
Result := ToHexString(M.AsAnsi(iText));
end;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment