Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ostream with bases other than 8, 10 and 16

Tags:

c++

base

ostream

I have a polynomial class, and its natural representation is its coefficients. If a coefficient is set, then its a 1 for binomial basis, 1 or 2 for trinomial basis, etc. For example, in a binomial basis, X2 + 1 is represented as 101; and in a trinomial basis, 2X2 + 1 is represented as 201.

The class provides an operator<< overload. Internally, the class represents the coefficients using an integral array. So I should be able to perform:

ostringstream oss;
for (size_t i=0; i<v.size(); i++)
   oss << v[i];

The problem I am having is I don't know how to configure the ostream for bases other than 8, 10 and 16. ios_base provides std::oct, std::dec and std::hex for the popular bases, but I don't see what to use for the less frequently used bases. And pages like C++ Reference on ios_base does not discuss what to use.

How do I use ostream with bases other than 8, 10 and 16?

like image 925
jww Avatar asked Nov 11 '15 21:11

jww


1 Answers

I don't think this is possible using standard means. Looking at std::setbase

Values of base other than 8, 10, or 16 reset basefield to zero, which corresponds to decimal output and prefix-dependent input.

like image 97
jaredready Avatar answered Sep 30 '22 21:09

jaredready