Enquiries on printf function

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2009
Posts: 39
Reputation: D.JOHN is an unknown quantity at this point 
Solved Threads: 0
D.JOHN D.JOHN is offline Offline
Light Poster

Enquiries on printf function

 
0
  #1
Sep 8th, 2009
To all the c++ experts,

I have some doubt on how does the convertion works.

If for instance, I want to convert a hexadecimal eg. 199 to to decimal,

I use printf("%d",199);

but what if I want the hexadecimal 199 to be displayed in binary?

What must is use?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 100
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Enquiries on printf function

 
1
  #2
Sep 8th, 2009
http://www.cplusplus.com/reference/c...cstdio/printf/

This is a complete explaination of the printf function . you will notice that %x or %X is denoted to show the hexadecimal notation.

Though I am unsure about being able to print out the binary value.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Enquiries on printf function

 
2
  #3
Sep 8th, 2009
but what if I want the hexadecimal 199 to be displayed in binary?
printf() does not have a conversion to binary. The C++ library has a bitset<> class that will indirectly do it for you, but you need to use cout to print directly, or manually convert to a string for printf():
  1. #include <iostream>
  2. #include <limits>
  3. #include <bitset>
  4.  
  5. int main()
  6. {
  7. std::cout << std::bitset<std::numeric_limits<int>::digits>(0x199) << '\n';
  8. }
The other alternative is manually converting the number to a binary string. If you search this forum you can find tons of threads about how to do it that way.
Last edited by Tom Gunn; Sep 8th, 2009 at 9:44 am.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 39
Reputation: D.JOHN is an unknown quantity at this point 
Solved Threads: 0
D.JOHN D.JOHN is offline Offline
Light Poster

Re: Enquiries on printf function

 
0
  #4
Sep 12th, 2009
I am a bit unclear about this line

std::cout << std::bitset<std::numeric_limits<int>::digits>(0x199) << '\n';

I know that because your do not use the namespace std hence your use std:: to replace it.

But if I am to write in this format:

#include <iostream>
#include <limits>
#include <bitset>
using namespace std; /*<--the thing that i mentioned*/

int main()
{
cout << bitset<numeric_limits<int>::digits>(0x199) << '\n';
}

am i correct?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 675
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 100
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Enquiries on printf function

 
0
  #5
Sep 12th, 2009
Yes, If you use the namespace. there is no need to inline the std::
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,346
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 168
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Enquiries on printf function

 
0
  #6
Sep 12th, 2009
Enquiries about printf function is more of a c question don't you think?
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 39
Reputation: D.JOHN is an unknown quantity at this point 
Solved Threads: 0
D.JOHN D.JOHN is offline Offline
Light Poster

Re: Enquiries on printf function

 
0
  #7
Sep 12th, 2009
Ok thank you very much
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC