Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-02

Format(), leading zeros, and hex digits

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mark Morgan Lloyd

Is it possible to get Format() to pad with leading zeros rather than spaces, particularly in the case of hex numbers? My understanding is that printf() interprets e.g. %02s as being padded with zeros.

In a similar vein, am I correct in understanding that FPC's Format() doesn't allow the case to be controlled in hex output? C's printf() distinguishes between %x and %X for case control.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Michael Van Canneyt
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote
Is it possible to get Format() to pad with leading zeros rather than spaces, particularly in the case of hex numbers? My understanding is that printf() interprets e.g. %02s as being padded with zeros.

For string arguments, this is not possible.
You can use %.2d for this, or %.2x for hex numbers:

araminta: >./tf
000F
araminta: >cat tf.pp
uses sysutils;

begin
   writeln(format('%.4x',[15]));
end.
In a similar vein, am I correct in understanding that FPC's Format() doesn't allow the case to be controlled in hex output? C's printf() distinguishes between %x and %X for case control.

You are correct in your understanding.

Michael.

Mark Morgan Lloyd
to Michael Van Canneyt
Michael Van Canneyt wrote 7 lines
On Sat, 22 Feb 2014, Mark Morgan Lloyd wrote: > Is it possible to get Format() to pad with leading zeros rather than > spaces, particularly in the case of hex numbers? My understanding is > that printf() interprets e.g. %02s as being padded with zeros. For string arguments, this is not possible.

My mistake, I meant %02x.

Show quoted text 10 lines
You can use %.2d for this, or %.2x for hex numbers: araminta: >./tf 000F araminta: >cat tf.pp uses sysutils; begin writeln(format('%.4x',[15])); end.

Thanks, I now see that is described in the example at http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/format.html but not in the preceding formal documentation.

Reminder noted with thanks.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]