std::bitset
has a .to_string()
method that returns a std::string
holding a text representation in binary, with leading-zero padding.
Choose the width of the bitset as needed for your data, e.g. std::bitset<32>
to get 32-character strings from 32-bit integers.
#include <iostream> #include <bitset> int main() { std::string binary = std::bitset<8>(128).to_string(); //to binary std::cout<<binary<<"\n"; unsigned long decimal = std::bitset<8>(binary).to_ulong(); std::cout<<decimal<<"\n"; return 0; }
EDIT: Please do not edit my answer for Octal and Hexadecimal. The OP specifically asked for Decimal To Binary.
Related Posts:
- Why unsigned int 0xFFFFFFFF is equal to int -1?
- How to reverse an std::string? [duplicate]
- Decimal to hex conversion c++ built-in function
- Integer to hex string in C++
- Fastest way to Convert String to Binary?
- Reading and writing binary file
- How to print (using cout) a number in binary form?
- Two decimal places using printf( )
- Pause Console in C++ program
- Why the switch statement cannot be applied on strings?
- ld: symbol(s) not found for architecture x86_64 error
- C++ string to double conversion
- How do I print out the contents of a vector?
- ‘was not declared in this scope’ error
- Good input validation loop using cin – C++
- C++ for each, pulling from vector elements
- How do I erase an element from std::vector<> by index?
- Incomplete Type Is Not Allowed
- how to implement quick sort algorithm in C++
- “No viable overloaded ‘=’ ” why?
- Replace part of a string with another string
- Error with multiple definitions of function
- What are C++ functors and their uses?
- invalid conversion from ‘const char*’ to ‘char*’
- Check if a string is palindrome
- What is an ‘undeclared identifier’ error and how do I fix it?
- Initializing a two dimensional std::vector
- What does “<<" and ">>” mean in C++ for cout/cin?
- C ++ error: a expected initializer before [function name]
- invalid new-expression of abstract class type
- std::string to char*
- Cleanest way to copy a constant size array in c++11
- Difference between using Makefile and CMake to compile the code
- Exception Error c0000005 in VC++
- push_back vs emplace_back
- Returning multiple values from a C++ function
- Eclipse C++ : “Program “g++” not found in PATH”
- no default constructor exists for class
- Press Enter to Continue
- How to trim a std::string?
- Parsing a comma-delimited std::string
- Random number c++ in some range
- Convert a String In C++ To Upper Case
- Is std::stoi actually safe to use?
- Visual Studio 2015 doesn’t have cl.exe
- Right way to split an std::string into a vector
- How to resize array in C++?
- C++: std does not have member “string”
- Fatal error: ‘stdafx.h’ file not found
- How do I add the MinGW bin directory to my system path?
- What’s the syntax for declaring an array of function pointers without using a separate typedef?
- What is the difference between const_iterator and non-const iterator in the C++ STL?
- Sorting Linked List C++ with pointers
- Get Unix timestamp with C++
- Class prototyping
- How to dynamically allocate an array of pointers in C++?
- Error C1083: Cannot open include file: ‘stdafx.h’
- How do you handle a “cannot instantiate abstract class” error in C++?
- How to cast the size_t to double or int C++
- How to get Current Directory?
- “used without template parameters”
- Reading from .txt file into two dimensional array in c++
- C++ Error ‘nullptr was not declared in this scope’ in Eclipse IDE
- C++ Simple hangman game
- “The system cannot find the file specified” when running C++ program
- Remove First and Last Character C++
- C++ Convert string (or char*) to wstring (or wchar_t*)
- Program received signal SIGSEGV, Segmentation fault
- Comparison of C++ unit test frameworks
- Why is inherited member not allowed?
- Error: macro names must be identifiers using #ifdef 0
- Error: free(): invalid next size (fast):
- Error: free(): invalid next size (fast):
- Reading data from file into an array
- LINK : fatal error LNK1561: entry point must be defined ERROR IN VC++
- Trying to use int in getline
- c++ error c2015: too many characters in constant
- Splitting a C++ std::string using tokens, e.g. “;”
- stoi function gives error: std::invalid_argument at memory location 0x0035E8D8. c++
- Stack around the variable ‘ ‘ was corrupted
- The difference between cin.ignore and cin.sync
- casting int to char using C++ style casting
- What is the job of autogen.sh when building a c++ package on Linux
- Using G++ to compile multiple .cpp and .h files
- LPCSTR, LPCTSTR and LPTSTR
- How to do std::string indexof in C++ that returns index of matching string?
- variable or field declared void
- Invalid conversion from ‘char’ to ‘const char *’
- How do I deal with “signed/unsigned mismatch” warnings (C4018)?
- Creating folders in C++
- Modulo operator with negative values [duplicate]
- Finding the type of an object in C++
- C++ Simple Dice roll – how to return multiple different random numbers [duplicate]
- How can I get a file’s size in C++?
- Why can’t I make a vector of references?
- What does “-Wall” in “g++ -Wall test.cpp -o test” do?
- C++ equivalent of StringBuffer/StringBuilder?
- heap corruption detected | C++
- Update g++ but still old version
- no matching function to call for “getline”