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


Groups > comp.lang.c > #10795

Re: fprintf internally supports print character array in hex?

From mathog <dmathog@gmail.com>
Newsgroups comp.lang.c
Subject Re: fprintf internally supports print character array in hex?
Date 2011-09-23 12:45 -0700
Organization A noiseless patient Spider
Message-ID <j5inkl$dr3$1@dont-email.me> (permalink)
References <j5ifbi$irm$1@dont-email.me> <0.60d7480fd9f3d3b77b0d.20110923184756BST.87sjnnw3oz.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse wrote:

> No.  The only "multiple" formats are for strings formatted as
> characters.

Well, that was sort of my point.  By (slightly odd) analogy, it is
as if the language implements

   od -c file

(via the %s format) but not

   od -x file

The only difference is how each char is formatted.  Ah, wait, I see why 
they didn't do it, the zero character is a string delimiter, so that 
sort of format string would not be very useful for printing general
hex data.  But printing strings which are not null terminated is also a 
problem.  Both of these could be handled by a width based %s variant like:

   fprintf(stdout,"%*sc",len,buffer);

Which would be "print to a field width of len using character formatting
from character data in array 'buffer'".  This would be equivalent to:

   fprintf(stdout,"%s",buffer);

when buffer is null terminated and len=strlen(buffer).  But the first 
form would work even if buffer contained nulls or was not terminated.

Similarly:

   fprintf(stdout,"%*sx",2*len,buffer);

would print the hex format (.2 form) of the characters.  And by 
extension, for wide chars

   fprintf(stdout,"%*lsx,4*len,buffer);

I guess %sb (binary)would also work, but not octal or decimal, since the 
mapping from character to subfield doesn't come out even for the latter 
two types.

>
> but doesn't your reference manual
> (online, local to your system, dead trees, whatever) answer this
> question for you?

I could not find anything obvious that described a way of doing this, 
but that doesn't mean that I didn't miss it, or that there was not some 
trick using the existing format options to accomplish this task.  So I 
asked.

Regards,

David Mathog

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