Null characters in strings

Frank Heckenbach ih8mj at fjf.gnu.de
Wed Sep 8 02:03:12 CEST 2010


Morton, John wrote:

> My thanks for confirming that writeln is not implemented using C's
> printf function (I can't remember where I got that idea) and that gpc's
> writeln can send all 256 characters (absolutely necessary to handle
> binary time as a string).
> 
> On reflection I realize I have misstated our problem.  But I think I
> also have a work-around for it. 
> 
> The issue is that the supplied library returns the time as a 32 bit
> binary value.  The problem is essentially how to coerce the 32 bits into
> a corresponding 4 character gpc string.  (BTW, they are the first 4
> bytes of every message sent by our application).
> 
> I had hoped that the coercion (effectively a type cast) would be easy to
> accomplish within Pascal.  I then thought about variant records but that
> did not seem feasible.  
> I have since realized that the conversion could be done where we define
> the interface to the external routine.  In the Pascal function
> definition use a 4 character fixed length string for the time field and
> have the linker map that string to the corresponding 32 bit value field
> used by the library (probably by using pass-by-address, possibly
> pass-by-value).  
> Not a pure Pascal approach but a practical one - as long as you are well
> aware of the pitfalls.  (In the "pure" approach the Pascal interface
> parameter would be a 32 bit unsigned value that gets converted/mapped by
> Pascal to the corresponding 4 byte string).

The only "pure" Pascal approach would be converting the value to
single bytes using "div"/"mod" (GPC also supports bitwise "and" and
"shl"/"shr") and then to characters using "Chr". If it's not
time-critical, this might actually be worth considering, since it
avoids endianness issues.

With GPC, also type-casting should work. Something like this:

program Foo;

type
  Card32 = Cardinal attribute (Size = 32);
  Char4 = array [1 .. 4] of Char;

var
  a: Card32;
  b: Char4;

begin
  b := Char4 (a);
  a := Card32 (b);
end.

Variant records are sometimes abused(!) for this purpose, but I
don't recommend it.

Frank

-- 
Frank Heckenbach, f.heckenbach at fh-soft.de, http://fjf.gnu.de/, 7977168E
GPC To-Do list, latest features, fixed bugs:
http://www.gnu-pascal.de/todo.html
GPC download signing key: ACB3 79B2 7EB2 B7A7 EFDE  D101 CD02 4C9D 0FE0 E5E8





More information about the Gpc mailing list