### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Wrong Coded AnsiString ToString ????  (Read 446 times)

#### Weitentaaal

• Full Member
• Posts: 185
##### Wrong Coded AnsiString ToString ????
« on: February 24, 2021, 03:42:37 pm »
My AnsiString is not Really a String (i guess, im don't know much about String encoding, mybe u can help me ? )

i Tryed:

Showmessage(Result.R01) and got 'TOT'
but if i tryed
Showmessage(Result.R01) in a loop i got nocthing

and after i tryed

ShowMessage (Result.R01) i got "?"

so i thought that my String is wrong coded.

Can Some1 Help please ?
My Code:

Code: Pascal  [Select][+][-]
1.
2. Function SWert(strWert: String; Position: Integer): String;
3. Var
4.    VStrWert, AcText: String;
5.    AcWert, AcPos: Integer;
6. Begin
7.    VStrWert := strWert;
8.    AcWert := 0;
9.    while True do Begin
10.       //MsgBox strWert
11.       //Open 'C:\Users\DominikR\Desktop\test.html' For Output: #1
12.       //Print #1, strWert
13.       //Close #1
14.       AcPos := Pos(';', VStrWert);
15.       AcText := ' ';
16.       If AcPos > 0 then Begin
17.          AcWert := AcWert + 1;
18.          AcText := LeftStr(VStrWert, AcPos - 1);
19.          VStrWert := RightStr(VStrWert, Length(VStrWert) - AcPos);
20.          If AcWert = Position then Begin
21.             If Length(AcText) = 0 then Begin
22.                AcText := ' ';
23.             End;
24.             Break;
25.          End;
26.       End Else Begin
27.          AcWert := AcWert + 1;
28.          If AcWert = Position then Begin
29.             If Length(VStrWert) > 0 then Begin
30.                AcText := VStrWert;
31.                VStrWert := '';
32.             End Else Begin
33.                AcText := ' ';
34.             End;
35.          End Else Begin
36.             AcText := ' ';
37.          End;
38.          Break;
39.       End;
40.    End;
41.    Result := AcText;
42. End;
43.
44. Function GetResponse(request : String): String;
45. var
46.   //WebString : AnsiString;
47.   //Adresse : String;
48.   HsGet: THTTPSend;
49.   isOK: Boolean;
50.   Resp: TStringList;
51. begin
52.   //Server Adresse BlackBox
54.   //TestString
55.   //WebString := '1;1;PT3012;1;30;1;5000;1.2;-10;50;25;0;0;70;50;40;0;840;1220;1000;2;0;0;0;1;0;0;0;0;0;0;0.12;0;10;0;0;0;0;40;40;;;0;;;;;;;;10:40:49';
56.   //WebString := request;
57.
58.   //request:= Adresse + 'resp.php?' + WebString;
59.   HsGet := THTTPSend.Create;
60.   Resp := TStringList.Create;
61.   try
62.      isOK := HsGet.HTTPMethod('GET', request);
63.      If isOK Then begin
65.      end else begin
66.         Result := 'Error';
67.      end;
68.   finally
69.      HsGet.Free;
70.   end;
71.
72.   //Stringliste aufarbeiten
73.   Result := Trim(Resp.Text);
74. end;
75.
76.
77. Function GetWebCalc(): BBWeb;
78. Var
79.    WebString, WegerServer, Rueckgabe: String;
80.    //StTime : TTimer;
81.    Msg: String;
82.    ByteArr : TByteArray;
83.    Temp : String;
84.    i : Integer;
85. Begin
86.    WegerServer := BBWebAdresse;
87.    WebString := BBWebData.S01 + ';';
88.    WebString := WebString + BBWebData.S02 + ';';
89.    .....
90.    WebString := WebString + BBWebData.S50 + ';' + DateToStr(Time);
91.
92.    ShowMessage(WegerServer + 'resp.php?' + WebString);
93.    Rueckgabe:= GetResponse(WegerServer + 'resp.php?' + WebString);
94.
95.    if (Rueckgabe = 'Error')then Application.MessageBox(PChar('Ein Fehler is Aufgetreten, Überprüfen Sie die Verbindung zum Server!'), PChar('BlackBox konnte keine Antwort liefern'), MB_ICONERROR);
96.
97.    ShowMessage(Rueckgabe);
98.    If Pos('xmlret', Rueckgabe) = 0 then Begin
99.       Result.R01 := SWert(Rueckgabe, 1);
100.      ....
101.       Result.R50 := SWert(Rueckgabe, 50);
102.       Result.RML := '';
103.    End Else Begin
104.       Result.R01 := '';
105.       .....
106.       Result.R50 := '';
107.       Result.RML := Rueckgabe;
108.    End;
109.
110.
111.    //Wrong Coded AnsiString (IF i do show this(ShowMessagebox): [result.R01[1]] i get "?" if i show the whole String i get 'TOT')
112.
113.
114.    //Temp:= Result.R01;
115.    //if(AnsiContainsStr(Result.R01, 'TOT'))then ShowMessage('acs');
116.    If((Trim(Result.R01) = 'TOT') or (Rueckgabe = ''))then Begin
117.       dbExecute(dbSet, 'SELECT * FROM Sprachen WHERE ID = 3100');
118.       Msg := dbSet.FieldByName(FeldD).AsString;
119.       dbExecute(dbSet, 'SELECT * FROM Sprachen WHERE ID = 1234');
120.       Application.MessageBox(PChar(Msg), PChar(dbSet.FieldByName(FeldD).AsString), MB_ICONERROR);
121.    End;
122. End;
123.
« Last Edit: February 24, 2021, 05:15:23 pm by Weitentaaal »
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0

#### VTwin

• Hero Member
• Posts: 1035
• Former Turbo Pascal 3 user
##### Re: Wrong Coded AnsiString ToString ????
« Reply #1 on: February 24, 2021, 04:32:10 pm »
Maybe someone can guess, but you need to show more of your code, including compiler settings and FPC version.

This link explains some of the details:

https://wiki.freepascal.org/Character_and_string_types
“Talk is cheap. Show me the code.” -Linus Torvalds

Free Pascal Compiler 3.2.0
macOS 10.13.6: Lazarus 2.0.12 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.12 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.12 (64 bit on VBox)

#### Martin_fr

• Hero Member
• Posts: 7161
• Debugger - SynEdit - and more
##### Re: Wrong Coded AnsiString ToString ????
« Reply #2 on: February 24, 2021, 05:18:34 pm »
So R01 in  "(Trim(Result.R01) = 'TOT')"  is everything up to the first ";".

The webserver may return Utf8 with none-printed codepoints, or a BOM.
Though a BOM would likely be visible in the debugger.

But there are any amount of none visible codepoints: LTR, RTL, zero-width space, ....
None of them would be handled by "Trim".

You need to get the hex content of all the bytes.

There is also the chance, that the "T" looks like a T, but is not the "latin T" in the ASCII range.
E.g. https://www.compart.com/en/unicode/U+FF34

This would not compare to the T in your constant.
And depending on settings, it could indeed be that if the string is translated to a diff codepage before it is compared, then it turns into a ? if it can non be represented in the target codepage
« Last Edit: February 24, 2021, 05:22:28 pm by Martin_fr »

#### Weitentaaal

• Full Member
• Posts: 185
##### Re: Wrong Coded AnsiString ToString ????
« Reply #3 on: February 25, 2021, 08:10:39 am »
It Worked with AnsiContainsStr(Result.R01, 'TOT')

Looked at this Function but didn't found out Why and what the coding of my Result.R01 looks like..

Can some1 Figure out whats going on or how to Recode this AnsiString to a UTF8String ??

Thanks !
Lazarus: 2.0.6 x86_64-win64-win32/win64
FPC-Version: 3.0.4
Compiler Version: 3.2.0