caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* sprintf-Bug?
@ 2007-02-04  1:00 Oliver Bandel
  2007-02-04  1:11 ` [Caml-list] sprintf-Bug? Jonathan Roewen
  0 siblings, 1 reply; 9+ messages in thread
From: Oliver Bandel @ 2007-02-04  1:00 UTC (permalink / raw)
  To: caml-list

Hello,

this I explored:

# let hex_of_byte b = Printf.sprintf "%02s" (Printf.sprintf "%0X" b);;                                        
val hex_of_byte : int -> string = <fun>
# hex_of_byte 12;;                                                                                            
- : string = " C"

Shouldn't the result be "0C" ?!
(I'm using OCaml 3.09.3.)


Ciao,
  Oliver


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] sprintf-Bug?
  2007-02-04  1:00 sprintf-Bug? Oliver Bandel
@ 2007-02-04  1:11 ` Jonathan Roewen
  2007-02-04  1:51   ` Oliver Bandel
  0 siblings, 1 reply; 9+ messages in thread
From: Jonathan Roewen @ 2007-02-04  1:11 UTC (permalink / raw)
  To: Oliver Bandel; +Cc: caml-list

Don't think so. I'm pretty sure padding of strings is only with
spaces. Padding of numbers has to be specified for a number format.

let hex_of_byte b = Printf.sprintf "%02X" b;;

Why you'd use two sprintfs is the real oddity =P

Jonathan

On 2/4/07, Oliver Bandel <oliver@first.in-berlin.de> wrote:
> Hello,
>
> this I explored:
>
> # let hex_of_byte b = Printf.sprintf "%02s" (Printf.sprintf "%0X" b);;
> val hex_of_byte : int -> string = <fun>
> # hex_of_byte 12;;
> - : string = " C"
>
> Shouldn't the result be "0C" ?!
> (I'm using OCaml 3.09.3.)
>
>
> Ciao,
>  Oliver
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] sprintf-Bug?
  2007-02-04  1:11 ` [Caml-list] sprintf-Bug? Jonathan Roewen
@ 2007-02-04  1:51   ` Oliver Bandel
  2007-02-04  1:55     ` Jonathan Roewen
                       ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Oliver Bandel @ 2007-02-04  1:51 UTC (permalink / raw)
  To: caml-list

On Sun, Feb 04, 2007 at 02:11:43PM +1300, Jonathan Roewen wrote:
> Don't think so. I'm pretty sure padding of strings is only with
> spaces.

The format-string "%02s" should make a '0' instead of ' '
as filling, IMHO.


> Padding of numbers has to be specified for a number format.

IMHO  "%2s" should make a string of at least two chars
length, fillingup with ' ' on the left side.

And "%0s" should make it with a filling-char '0'
instead of filling-chars ' '.



> 
> let hex_of_byte b = Printf.sprintf "%02X" b;;
> 
> Why you'd use two sprintfs is the real oddity =P

The inner sprintf makes hex from int and the outer
makes two-char-string instead of one-char-string.

Or how to make hex-output with filling '0' instead
of ' ' for two-char length strings?

Would a "%00X" work?!


Ciao,
   Oliver


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] sprintf-Bug?
  2007-02-04  1:51   ` Oliver Bandel
@ 2007-02-04  1:55     ` Jonathan Roewen
       [not found]     ` <45C53E45.1080109@rftp.com>
  2007-02-04 10:41     ` Christophe TROESTLER
  2 siblings, 0 replies; 9+ messages in thread
From: Jonathan Roewen @ 2007-02-04  1:55 UTC (permalink / raw)
  To: Oliver Bandel; +Cc: caml-list

I gave the example :P "%02X" prints a number as hex, 2 chars in length
(assuming no more than 2 chars wide), padded with zeroes. Same as
you'd do in any other lang with C-style printf.

On 2/4/07, Oliver Bandel <oliver@first.in-berlin.de> wrote:
> On Sun, Feb 04, 2007 at 02:11:43PM +1300, Jonathan Roewen wrote:
> > Don't think so. I'm pretty sure padding of strings is only with
> > spaces.
>
> The format-string "%02s" should make a '0' instead of ' '
> as filling, IMHO.
>
>
> > Padding of numbers has to be specified for a number format.
>
> IMHO  "%2s" should make a string of at least two chars
> length, fillingup with ' ' on the left side.
>
> And "%0s" should make it with a filling-char '0'
> instead of filling-chars ' '.
>
>
>
> >
> > let hex_of_byte b = Printf.sprintf "%02X" b;;
> >
> > Why you'd use two sprintfs is the real oddity =P
>
> The inner sprintf makes hex from int and the outer
> makes two-char-string instead of one-char-string.
>
> Or how to make hex-output with filling '0' instead
> of ' ' for two-char length strings?
>
> Would a "%00X" work?!
>
>
> Ciao,
>   Oliver
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] sprintf-Bug?
       [not found]     ` <45C53E45.1080109@rftp.com>
@ 2007-02-04  2:08       ` Oliver Bandel
  2007-02-04  2:11         ` Jonathan Roewen
  0 siblings, 1 reply; 9+ messages in thread
From: Oliver Bandel @ 2007-02-04  2:08 UTC (permalink / raw)
  To: caml-list

On Sat, Feb 03, 2007 at 06:00:37PM -0800, Robert Roessler wrote:
> Oliver Bandel wrote:
> >...
> >Or how to make hex-output with filling '0' instead
> >of ' ' for two-char length strings?
> >
> >Would a "%00X" work?!
> 
> Nope, just use "%02X".
[...]


Oh, cool.

I thought I had already tried it and it didn't worked.
But I just tried it at my Mac and it did work this way.

I will try this again on the Sun next week, where I
thought that I already tried it and it didn't worked...

( ...but maybe I was tired only ;-) and typed something stupid ;-) )


But why didn't worked the code with the two sprintf's?!
The outer sprintf should have do what I was looking for?!

Thanks,
   Oliver


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] sprintf-Bug?
  2007-02-04  2:08       ` Oliver Bandel
@ 2007-02-04  2:11         ` Jonathan Roewen
  2007-02-04 12:43           ` Oliver Bandel
  0 siblings, 1 reply; 9+ messages in thread
From: Jonathan Roewen @ 2007-02-04  2:11 UTC (permalink / raw)
  To: Oliver Bandel; +Cc: caml-list

No. Padding for strings only adds spaces. It's the design. You can
check the manual if you want. Padding with zeroes applies solely to
numerical conversions.

On 2/4/07, Oliver Bandel <oliver@first.in-berlin.de> wrote:
> On Sat, Feb 03, 2007 at 06:00:37PM -0800, Robert Roessler wrote:
> > Oliver Bandel wrote:
> > >...
> > >Or how to make hex-output with filling '0' instead
> > >of ' ' for two-char length strings?
> > >
> > >Would a "%00X" work?!
> >
> > Nope, just use "%02X".
> [...]
>
>
> Oh, cool.
>
> I thought I had already tried it and it didn't worked.
> But I just tried it at my Mac and it did work this way.
>
> I will try this again on the Sun next week, where I
> thought that I already tried it and it didn't worked...
>
> ( ...but maybe I was tired only ;-) and typed something stupid ;-) )
>
>
> But why didn't worked the code with the two sprintf's?!
> The outer sprintf should have do what I was looking for?!
>
> Thanks,
>   Oliver
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] sprintf-Bug?
  2007-02-04  1:51   ` Oliver Bandel
  2007-02-04  1:55     ` Jonathan Roewen
       [not found]     ` <45C53E45.1080109@rftp.com>
@ 2007-02-04 10:41     ` Christophe TROESTLER
  2 siblings, 0 replies; 9+ messages in thread
From: Christophe TROESTLER @ 2007-02-04 10:41 UTC (permalink / raw)
  To: caml-list

On Sun, 4 Feb 2007, Oliver Bandel <oliver@first.in-berlin.de> wrote:
> 
> And "%0s" should make it with a filling-char '0'
> instead of filling-chars ' '.

No, reread the documentation:

   - [0]: for numerical conversions, pad with zeroes instead of spaces.
          ^^^^^^^^^^^^^^^^^^^^^^^^^
%s is not a numerical conversion

> Or how to make hex-output with filling '0' instead
> of ' ' for two-char length strings?

   Printf.sprintf "%02X" 12

You have to tell the length here otherwise no filling is necessary.

ChriS


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] sprintf-Bug?
  2007-02-04  2:11         ` Jonathan Roewen
@ 2007-02-04 12:43           ` Oliver Bandel
  0 siblings, 0 replies; 9+ messages in thread
From: Oliver Bandel @ 2007-02-04 12:43 UTC (permalink / raw)
  To: caml-list

On Sun, Feb 04, 2007 at 03:11:29PM +1300, Jonathan Roewen wrote:
> No. Padding for strings only adds spaces. It's the design. You can
> check the manual if you want. Padding with zeroes applies solely to
> numerical conversions.
[...]

Oh,strange.

I'm used to have theese '0'-paddings, e.g. from Perl.
And they make sense to me.

Ciao,
   Oliver


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [Caml-list] sprintf-Bug?
       [not found] <20070204020819.B999DBC6F@yquem.inria.fr>
@ 2007-02-04 10:04 ` David Allsopp
  0 siblings, 0 replies; 9+ messages in thread
From: David Allsopp @ 2007-02-04 10:04 UTC (permalink / raw)
  To: caml-list

> > Oliver Bandel wrote:
> But why didn't worked the code with the two sprintf's?!
> The outer sprintf should have do what I was looking for?!

Printf.sprintf "%0X" converts a number to a capitalised hex *string* (the 0
is pointless without a width specification --- there will never be padding)

Printf.sprintf "%02s" inserts a *string* ensuring that it is at least two
characters long. The '0' is ignored --- the O'Caml manual states that the
'0' and '+' flags are for numerical conversion only
(http://caml.inria.fr/pub/docs/manual-ocaml/libref/Printf.html). I guess you
could argue that "%02s" is not a valid format and so should raise 

This is also the behaviour of ISO C sprintf
(http://www.opengroup.org/onlinepubs/009695399/functions/printf.html) 

> Thanks,
>    Oliver


David


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2007-02-04 12:43 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-04  1:00 sprintf-Bug? Oliver Bandel
2007-02-04  1:11 ` [Caml-list] sprintf-Bug? Jonathan Roewen
2007-02-04  1:51   ` Oliver Bandel
2007-02-04  1:55     ` Jonathan Roewen
     [not found]     ` <45C53E45.1080109@rftp.com>
2007-02-04  2:08       ` Oliver Bandel
2007-02-04  2:11         ` Jonathan Roewen
2007-02-04 12:43           ` Oliver Bandel
2007-02-04 10:41     ` Christophe TROESTLER
     [not found] <20070204020819.B999DBC6F@yquem.inria.fr>
2007-02-04 10:04 ` David Allsopp

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).