pchar conversion lazarus / 61-Beginners ---------------------------------------------------------------------- turunk · 2025-05-03 13:57 i want to convert a string to pchar. is the #0 automatic? is this a new memory-address? i want to convert a pchar to string. case sensitive. i use strcat to append. in which library they are? most basic-est. if i p2:=p1 , is p2 a new memory-address? is p2:=p1+10 , the 11th letter-address? ---------------------------------------------------------------------- TRon · 2025-05-03 14:04 Do you really need to use/work with pchar'ed strings ? A cast to pchar is usually enough (and yes the 0-ending is automatic in such case). If not then f.e. unit strings. ---------------------------------------------------------------------- Thaddy · 2025-05-03 14:12 The #0 is indeed always added, but not counted towards the length. That is just because of making the PChar cast possible. It is more efficient, though, to first concat() or + real Pascal strings before the cast to PChar. Pascal strings are more efficient than PChars in determining string length (one read (O(1)) vs strlen (O(n)). C'isms aren't always beneficial. That is a common mistake. In heavy string oriented applications, use Pascal string types. If you really need a PChar, e.g. for an external library, convert the end result to a PChar, not anything intermediate. But note C++, not C, has string classes that resemble Pascal strings. Alas, those are not directly compatible. ---------------------------------------------------------------------- ASerge · 2025-05-03 14:28 Quote from: turunk on May 03, 2025, 03:57:01 pmi want to convert a string to pchar. is the #0 automatic? is this a new memory-address? Yes, #0 is always at the end of the string. No, it's the same address as the string. Quotei want to convert a pchar to string. case sensitive. It's same chars. Quotei use strcat to append. It's error. Consider that after being cast to PChar, the pointer points to immutable characters. Quotein which library they are? most basic-est. PChar or string? Quoteif i p2:=p1 , is p2 a new memory-address? No. Quoteis p2:=p1+10 , the 11th letter-address? Yes. ---------------------------------------------------------------------- turunk · 2025-05-03 18:19 thanks and the procedures to convert string to pchar and pchar to string are in which library? is strcat not doing pchar1-content+no #0+pchar2-content+#0 ? ---------------------------------------------------------------------- Thaddy · 2025-05-03 18:32 String to Pchar is a cast, no routines involved. That is because of the trailing zero. PChar to string has setstring and strpas in the system unit, not in any library, for pchar to shortstring use strpcopy(), not a cast: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program testpands;{$mode objfpc}{$H+}var  a,b:PChar;  c:string;  d:Shortstring;begin  a:='test this a bit';  writeln(gettypekind(a)); //pchar is a pointer type  writeln(a);  SetString(c,a,strlen(a)); // make the Pchar into an Ansi string  // or cast:  c:=string(a);  writeln(gettypekind(c)); // c is an ansistring  writeln(c);  b:=PChar(c); // the cast does not generate extra code.  writeln(gettypekind(b)); // pchar is a pointer type  writeln(b);  d:=strpas(b);  // convert pchar to shortstring  Writeln(gettypekind(d));end. ---------------------------------------------------------------------- jamie · 2025-05-03 18:35 Quote from: turunk on May 03, 2025, 08:19:10 pmthanks and the procedures to convert string to pchar and pchar to string are in which library? is strcat not doing pchar1-content+no #0+pchar2-content+#0 ? Open the Help and go for the FPC/RTL help, type in the search for "pchar" and "Str" and see the list of functions that deal with this.