I am having trouble converting text within an edit box to a WideChar. This is being used in code for printing emoji characters.
If I manually set the WideChar values like the following it works
Emoji[1] := WideChar($D83D);
Emoji[2] := WideChar($DC4D);
But I want to be able to set the hex codes via edit boxes as follows
StringToWideChar(edit1.text, @wc1, Length(edit1.text));
StringToWideChar(edit2.text, @wc2, Length(edit2.text));
Emoji[1] := wc1;
Emoji[2] := wc2;
wc1 and wc2 are defined as WideChar. The edit boxes contain the same values as are hard coded above. That code results in a blank output, so something is wrong with the conversion.
What am I doing wrong? Thanks for any help here.
You mustn't interpret the string '$D83D'
as text -- instead, you must parse it as an integer.
First, you need to obtain the text from the edit box. This is Edit1.Text
. Then you need to convert this to an integer. For instance, you can use StrToInt
or TryStrToInt
. Then you simply need to reinterpret (cast) this integer as a Char
:
procedure TForm1.Edit1Change(Sender: TObject);
var
CodeUnit: Integer;
begin
if TryStrToInt(Edit1.Text, CodeUnit) and InRange(CodeUnit, 0, $FFFF) then
Label1.Caption := Char(CodeUnit)
else
Label1.Caption := '';
end;
Here, as a bonus, I also validate that the supposed codeunit is an actual 16-bit unsigned integer using InRange
(I mean, the user could in theory type 123456789
). Delphi's StrToInt
functions support hex using the dollar sign notation.