943,670 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 605
  • C++ RSS
Sep 8th, 2009
0

Enquiries on printf function

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
D.JOHN is offline Offline
39 posts
since Aug 2009
Sep 8th, 2009
1

Re: Enquiries on printf function

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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Sep 8th, 2009
2

Re: Enquiries on printf function

Quote ...
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():
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Sep 12th, 2009
0

Re: Enquiries on printf function

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?
Reputation Points: 10
Solved Threads: 0
Light Poster
D.JOHN is offline Offline
39 posts
since Aug 2009
Sep 12th, 2009
0

Re: Enquiries on printf function

Yes, If you use the namespace. there is no need to inline the std::
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Sep 12th, 2009
0

Re: Enquiries on printf function

Enquiries about printf function is more of a c question don't you think?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Sep 12th, 2009
0

Re: Enquiries on printf function

Ok thank you very much
Reputation Points: 10
Solved Threads: 0
Light Poster
D.JOHN is offline Offline
39 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: 1540-0063 (S) The text "eventqueue_t" is unexpected.
Next Thread in C++ Forum Timeline: c++ OOP Mortgage Calculator





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC