Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #11104

Re: fprintf internally supports print character array in hex?

From "BartC" <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: fprintf internally supports print character array in hex?
Date 2011-09-30 12:36 +0100
Organization A noiseless patient Spider
Message-ID <j649l1$st1$1@dont-email.me> (permalink)
References <j5ifbi$irm$1@dont-email.me> <0.60d7480fd9f3d3b77b0d.20110923184756BST.87sjnnw3oz.fsf@bsb.me.uk> <j5inkl$dr3$1@dont-email.me> <0.8ef2a8c8cc18ef49056a.20110923213440BST.87boubvvz3.fsf@bsb.me.uk> <j5j3oo$49s$1@dont-email.me>

Show all headers | View raw


"mathog" <dmathog@gmail.com> wrote in message 
news:j5j3oo$49s$1@dont-email.me...

> Anyway, a more generic format specifier might look like
>
>   %repeat#(%<modifiers>format(datatype(input)))

> Allow nested repeats and access to the (implicit) input indices and it 
> becomes more powerful, but the syntax gets complicated.  Here's hoping I 
> counted parens correctly:
>
>   general_print(stdout,
>     "%#(1)(%3d(i2)  %5#(%3x(i(2)):%2d(l(3)), )%3x((2)):%2d(l(3))\n)",
>      len,buffer1,buffer2);

It's more powerful, but also impossible to understand.

> which if len was 4 would print:
>   0  xxx:yy, xxx:yy, xxx:yy, xxx:yy, xxx:yy, xxx:yy
>   5  xxx:yy, xxx:yy, xxx:yy, xxx:yy, xxx:yy, xxx:yy
>  10  xxx:yy, xxx:yy, xxx:yy, xxx:yy, xxx:yy, xxx:yy
>  15  xxx:yy, xxx:yy, xxx:yy, xxx:yy, xxx:yy, xxx:yy
>
> where the xxx values are hex taken as ints from buffer 1 and yy are
> decimal values as longs from buffer2.

A more conventional approach might look like:

 for (i=0; i<len; ++i) {
    if ((i%5)==0) printf("%4d ",i);
    printf("%03d:%02d",buffer1[i],buffer2[i]);
    printf("%s",(i+1)%5!=0?", ":"\n");
 }

which at least is possible to debug if it's not quite right. And typos are 
more likely to be picked up at compile time.

> And that's enough speculation for one day.

I think I once proposed an array format-specifier, but can't remember the 
details. But based on your idea, it might look like:

 printf ("(%5#, %d)", array);  or printf("(%*#, %d)",5,array);

which might print out (10,20,30,40,50). Only repeat count and separator 
string are specified. And the array elements must of the type implied by the 
format.

Anything more ambitious I think would quickly get too complex.

-- 
Bartc 

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

fprintf internally supports  print character array in hex? mathog <dmathog@gmail.com> - 2011-09-23 10:23 -0700
  Re: fprintf internally supports  print character array in hex? blp@cs.stanford.edu (Ben Pfaff) - 2011-09-23 10:32 -0700
    Re: fprintf internally supports  print character array in hex? mathog <dmathog@gmail.com> - 2011-09-23 10:38 -0700
  Re: fprintf internally supports  print character array in hex? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-23 18:47 +0100
    Re: fprintf internally supports  print character array in hex? mathog <dmathog@gmail.com> - 2011-09-23 12:45 -0700
      Re: fprintf internally supports  print character array in hex? blp@cs.stanford.edu (Ben Pfaff) - 2011-09-23 12:50 -0700
      Re: fprintf internally supports  print character array in hex? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-23 21:34 +0100
        Re: fprintf internally supports  print character array in hex? mathog <dmathog@gmail.com> - 2011-09-23 16:12 -0700
          Re: fprintf internally supports print character array in hex? Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-09-30 01:06 -0700
          Re: fprintf internally supports  print character array in hex? "BartC" <bc@freeuk.com> - 2011-09-30 12:36 +0100
        Re: fprintf internally supports  print character array in hex? David Thompson <dave.thompson2@verizon.net> - 2011-10-03 01:11 -0400
    Re: fprintf internally supports  print character array in hex? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-09-29 21:41 +0000
  Re: fprintf internally supports print character array in hex? Gene <gene.ressler@gmail.com> - 2011-10-03 02:26 -0700

csiph-web