Exact binary in C++

This is a discussion on Exact binary in C++ within the C++ Programming forums, part of the General Programming Boards category; I made a function to change from char to binary, but i can't show it with zero number in front ...

  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,006
    >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
    21,311
    You can calculate and then convert and display as needed.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,793
    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, 01:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 10: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, 08:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21