Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,261 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,533 people online right now. Registration is fast and FREE... Join Now!




scanf()

 
Reply to this topicStart new topic

scanf(), how to read in a binary value

coRPse
15 Oct, 2006 - 07:50 AM
Post #1

New D.I.C Head
*

Joined: 29 Sep, 2006
Posts: 21



Thanked: 1 times
My Contributions
hey, im doing a binary to decimal conversion, and I wrote some code which will work, if I can read in a binary number from the console window.

The problem i'm having is that I type '1010', meaning base2, but obviously C reads it in through scanf() as being one thousand and ten [base10].

Is there an inbuilt function which means C reads a binary number as a binary number? kind of like scanf("%o" &oct) would read in an octal number.

do i need to write one myself?
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Scanf()
15 Oct, 2006 - 08:46 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
You can try adapting something using strtol():

http://www.cplusplus.com/ref/cstdlib/strtol.html

but the common method is to accept it as an integer (long) or string and convert, I'm afraid.
User is offlineProfile CardPM
+Quote Post

coRPse
RE: Scanf()
15 Oct, 2006 - 08:58 AM
Post #3

New D.I.C Head
*

Joined: 29 Sep, 2006
Posts: 21



Thanked: 1 times
My Contributions
Ah sucks. thats what I was afraid of tongue.gif

ta for the help though, that looks a useful function smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 06:57PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month