printf format question 
Author Message
 printf format question

Hi,

Using...  
 printf("%i:\t0x%2x\n", i, a_packet.data[i]);
                   =====
The 0x%2x format gives results like "0x a". Is there any way to pad with
zero so output is "0x0a"? I've looked through K&R2 and had a quick
browse of the faq but didn't come up with anything. Thanks, matthew.



Tue, 05 Apr 2005 05:22:07 GMT  
 printf format question

Quote:

> Hi,

> Using...
>  printf("%i:\t0x%2x\n", i, a_packet.data[i]);
>                    =====
> The 0x%2x format gives results like "0x a". Is there any way to pad with
> zero so output is "0x0a"? I've looked through K&R2 and had a quick
> browse of the faq but didn't come up with anything. Thanks, matthew.

%02x

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Tue, 05 Apr 2005 05:22:27 GMT  
 printf format question

Quote:

> > Hi,

> > Using...
> >  printf("%i:\t0x%2x\n", i, a_packet.data[i]);
> >                    =====
> > The 0x%2x format gives results like "0x a". Is there any way to pad with
> > zero so output is "0x0a"? I've looked through K&R2 and had a quick
> > browse of the faq but didn't come up with anything. Thanks, matthew.

> %02x

Or %.2x

--
Peter



Tue, 05 Apr 2005 05:51:06 GMT  
 printf format question

Quote:

>> Hi,

>> Using...
>>  printf("%i:\t0x%2x\n", i, a_packet.data[i]);
>>                    =====
>> The 0x%2x format gives results like "0x a". Is there any way to pad with
>> zero so output is "0x0a"? I've looked through K&R2 and had a quick
>> browse of the faq but didn't come up with anything. Thanks, matthew.

> %02x

How about
%#.2x
?
--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond


Tue, 05 Apr 2005 16:29:32 GMT  
 printf format question

Quote:


<snip>

> > %02x

> How about
> %#.2x
> ?

<shrug> Whichever you find easiest to remember, I guess. I think the
first looks simpler, but YMMV.

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton



Tue, 05 Apr 2005 17:32:50 GMT  
 printf format question

Quote:
>Using...  
> printf("%i:\t0x%2x\n", i, a_packet.data[i]);
>                   =====
>The 0x%2x format gives results like "0x a". Is there any way to pad with
>zero so output is "0x0a"? I've looked through K&R2 and had a quick
>browse of the faq but didn't come up with anything. Thanks, matthew.

You didn't look hard enough through K&R2.  Page 243 is offerring you no
less than TWO solutions: "0x%02x" and "%#04x" and the next page is
offering you one more: "0x%.2x", yet you somehow managed to miss ALL of
them!  Are you sure you've really looked through K&R2?

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Tue, 05 Apr 2005 18:52:16 GMT  
 printf format question

Quote:


>>> Hi,

>>> Using...
>>>  printf("%i:\t0x%2x\n", i, a_packet.data[i]);
>>>                    =====
>>> The 0x%2x format gives results like "0x a". Is there any way to pad with
>>> zero so output is "0x0a"? I've looked through K&R2 and had a quick
>>> browse of the faq but didn't come up with anything. Thanks, matthew.

>> %02x

>How about
>%#.2x
>?

It's the most esoteric of all the solutions: how many people can remember
the effect of the # flag in all the allowed contexts?  Or even enumerate
the allowed contexts?

Apart from that, the solution is correct, of course.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Tue, 05 Apr 2005 18:58:29 GMT  
 printf format question

Quote:



> >How about
> >%#.2x
> Apart from that, the solution is correct, of course.

Unless the OP needs 0x00 instead of 00 when a_packet.data[i] is 0.

--
G?ran Larsson     http://www.mitt-eget.com



Tue, 05 Apr 2005 22:01:32 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. A printf format specifier question:

2. tiny question on format of scanf and printf

3. printf format/data representation: question

4. printf() float format question

5. replacing printf with my printf, then calling C lib's printf

6. printf formats floats wrong?

7. printf() format extensions - looking for beta testers...

8. Printf in tabular format

9. Format string as a parameter to printf()?

10. Problems with printf conversion formats

11. Format to printf a double

12. Format a decimal conversion with thousands grouping (printf)

 

 
Powered by phpBB® Forum Software