@JLWest
I think I have solved you issue.
Using my Character Map, these are what I found:
- ASCII #128 .. #191 will be mapped to #194 + C
- ASCII #192 .. #255 will be mapped to #195 + (C-64)
- ASCII #127 .. #160 are
non-displayable characters (at least on my system)
My solution is to write 2 functions:
ASCII2UTF8 and
UTF82ASCII:
function ASCII2UTF8(C: Char): string;
begin
Result := '';
case C of
#128..#191 : Result := chr(194) + C;
#192..#255 : Result := chr(195) + chr(Ord(C)-64);
else
Result := C;
end;
end;
function ASCII2UTF8(B: Byte): string;
begin
Result := ASCII2UTF8(chr(B));
end;
function UTF82ASCII(const S: string): Char;
var
C1, C2: Char;
begin
Result := #0;
if Length(S) <= 1 then
begin
if S = '' then Exit;
Result := S[1];
Exit;
end;
C1 := S[1];
C2 := S[2];
case C1 of
#194 : Result := C2;
#195 : Result := chr(Ord(C2)+64);
end;
end;
My solution was only tested on Ubuntu Mate GTK2, it may or may not works on Windows. Also, it does not try to
correctly remap the characters $7F..$A0 as they are non displayable on my system (see img2), I have no clue how to map them.
Below is the whole source code:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, StdCtrls, StrUtils;
type
{ TForm1 }
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
function ASCII2UTF8(C: Char): string;
begin
Result := '';
case C of
#128..#191 : Result := chr(194) + C;
#192..#255 : Result := chr(195) + chr(Ord(C)-64);
else
Result := C;
end;
end;
function ASCII2UTF8(B: Byte): string;
begin
Result := ASCII2UTF8(chr(B));
end;
function UTF82ASCII(const S: string): Char;
var
C1, C2: Char;
begin
Result := #0;
if Length(S) <= 1 then
begin
if S = '' then Exit;
Result := S[1];
Exit;
end;
C1 := S[1];
C2 := S[2];
case C1 of
#194 : Result := C2;
#195 : Result := chr(Ord(C2)+64);
end;
end;
procedure TForm1.ListBox1Click(Sender: TObject);
Var
Item : string;
Bit1 : string;
i : Integer = -1;
begin
i := ListBox1.ItemIndex;
if (i = -1) or (i = 0) then Exit;
Item := Listbox1.Items[i];
Bit1 := Copy2SpaceDel(item);
Item := Trim(Item);
Bit1 := Copy2SpaceDel(item);
Item := Trim(Item);
Bit1 := Copy2SpaceDel(item);
Item := Trim(Item);
Label1.Caption := Item;
Edit1.Text := Item;
Edit2.Text := Ord(UTF82ASCII(Item)).ToString;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
i: Integer;
begin
ListBox1.Items.Add('Ascii ' + IntToStr(32) + ' = ' + 'Space');
for i := 33 to 255 do
ListBox1.Items.Add('Ascii ' + IntToStr(i) + ' = ' + ASCII2UTF8(i));
end;
end.