3

I am trying to produce binary numbers using C's itoa function and C++ setfill and setw function. If I use only itoa, the output displayed does not have proper 0 padding.

This is a small code snippet.

int s = 8; for (int i = 1; i<s;i++) { itoa(i,buffer,2); cout<<setfill('0')<<setw(3)<<endl; cout<<buffer<<endl; } 

Now it does a great job in printing out the output.

If I hadn't used setfill and setw, the formatting would have been something like

1 10 11 100 101 110 111 

instead of

001 010 011 100 101 110 111 

Now I want to store the padded binary numbers produced and store it into a vector. Is it possible?

I think I have got a solution using bitset, and it works fine.

 std::ostringstream oss; int s = 3; for (int i = 1; i<s;i++) { itoa(i,buffer,2); oss<<setfill('0')<<setw(3); oss<<buffer; string s = oss.str(); cout<<s<<'\n'<<endl; }; 

However, I just want to point out that the solution I obtained looks some this!Bin

Can it manipulated by flushing out streams in consecutive iterations. Its just an afterthought.

2
  • 2
    Are you looking for std::ostringstream? Commented Dec 23, 2014 at 12:57
  • 1
    AFAIK, there is no itoa() in c99 standard. maybe you're using a compiler extension that supports this function. Commented Dec 23, 2014 at 13:04

1 Answer 1

3

Consider using a bitset instead of itoa:

#include <bitset> #include <iostream> #include <string> #include <vector> int main() { std::vector<std::string> binary_representations; int s = 8; for (int i = 1; i < s; i++) { binary_representations.push_back(std::bitset<3>(i).to_string()); } } 

EDIT: If you need a variable length, one possibility is

// Note: it might be better to make x unsigned here. // What do you expect to happen if x < 0? std::string binary_string(int x, std::size_t len) { std::string result(len, '0'); for(std::string::reverse_iterator i = result.rbegin(); i != result.rend(); ++i) { *i = x % 2 + '0'; x /= 2; } return result; } 

and then later

binary_representations.push_back(binary_string(i, 3)); 
Sign up to request clarification or add additional context in comments.

9 Comments

Yes, thanks for the reply, it works, but do you want to anything about why shouldn't I use itoa.
Mainly because it's easier (it already does exactly what you want). In general, though, it's best to avoid working with C-style strings in C++ unless you have a really good reason to use them because it's so easy to go wrong with them -- your buffer could be too short, you could forget zero-terminators (thanks, snprintf!), you can forget to free what you strdup, that sort of thing. If you're using equivalent C++ constructs, those kinds of errors are harder to make. Oh, and itoa in particular isn't very portable (I don't have it on Linux).
Sorry, just realizing it later, what does 3 in bitset<3> represents- is it the fact that 2^3 = 8? And also can 3 be replaced by a variable? Thanks.
It means that the bitset has three bits. So...yes, actually, but the reason I suggested using it is that the string representation of a bitset<N> is N characters long regardless of the number it represents. Which I suppose is the same thing said in a different way.
I want to use it like this int x = 4; bitset<x>(1).to_string(). Is it possible.Visual Studio is saying that bitset< > only accepts constants, not variables.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.