Thread: Exact binary in C++

  1. #1
    Registered User vddking2's Avatar
    Join Date
    Nov 2006
    Location
    Vietnam
    Posts
    12

    Unhappy Exact binary in C++

    I made a function to change from char to binary, but i can't show it with zero number in front of result.

    Example: it show 10001 while I would like it show: 0010001

    How can I do it?

    Thanks!

    Vietnamese semi pro ^^

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >How can I do it?

    Modify your code.

    If you want help with your code, I would suggest that you post it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    55
    You could make a string with as many 0's as you want

  4. #4
    Registered User vddking2's Avatar
    Join Date
    Nov 2006
    Location
    Vietnam
    Posts
    12
    But i want to use it to calculate, so I can't change it to string

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can calculate and then convert and display as needed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Calculate the length of your binary string. If it isn't the correct length then modify the string to include a couple more 0's.
    My computer is awesome.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    How is the converted binary value stored? Do you convert it and put it in an int? If it's purely a question of displaying the result with leading zeros, then you should be able to use the stream manipulator functions setw and setfill (along with maybe right... or is it left?) to achieve that result.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM