Boost logo

Boost :

From: Dietmar Kuehl (dietmar_kuehl_at_[hidden])
Date: 2000-09-25 16:09:09


Hi,
Daryle Walker wrote:
> Am I right to assume that only the decimal base gets printing of
> signs ("-" or "+") decimal points and thousands separators?

Yes. Whether the positive sign is printed depends on the setting of
'showpos' in 'std::ios_base::fmtflags()'. Another setting applicable to
printing of integers is 'std::ios_base::uppercase' to determine the case
of letters with hexadecimal formatting.

> And that the octal and hexadecimal get none of those; they are just
> compact printings of a number's binary representation (at most they
> get a preceding "0" or "0x" to indicate which base).

That is my understanding. The printing of the '0' or '0x'/'0X' depends
on the flag 'std::ios_base::showbase' (and, of course, the case of the
'X' depends on 'std::ios_base::uppercase').

> I know that locale can provide help in determining if a character is
> a digit (possibly hexadecimal), but how do I know which digit that
> character represents? Without that, I might as well keep checking
> for ['0'..'9', 'A'..'F', 'a'..'f'] manually.

It basically comes down to that: You use the 'std::ctype' facet to
'widen()' the digit characters. This can then be used to lookup the
corresponding character:

  cT lower[16];
  cT upper[16];
  std::ctype<cT> const& ct = std::use_facet<std::ctype<cT> >(loc);
  char const clower[] = "0123456789abcdef";
  char const cupper[] = "0123456789ABCDEF";
  ct.widen(begin(clower), end(clower), begin(lower));
  ct.widen(begin(cupper), end(cupper), begin(upper));

  cT const* values = ct.is(std::ctype_base::lower, digit)
                     ? lower
                     : upper;

  int val = std::find(values, values + 16, digit)) - values;
  if (val < 16)
    std::cout << "digit value is " << val << "\n";
  else
    std::cout << "'" << digit << "' is not a digit\n";

Of course, the widening of the digits should be done in some form of
preprocessing...

> Do the numeric locale facets only apply to decimal-represented
> numbers, or can they help with octal and hexadecimal formats?

The only numeric facet applicable is 'std::numpunct' and this only has
values useful for decimal representation, ie. grouping and thousands
separators.

> Can these facets help with writing/reading user-defined types,
> besides providing the decimal point, thousands separator, and
> groupings? I know that those facets can write or read (unsigned)
> longs completely, but can they help read bigger numbers (possibly
> piecewise)?

They cannot help you in formatting of other numeric types than those
defined in these facets. However, I would consider to define a new
facet for the formatting of new numeric types: This way, a user can
modify the format if a different format is desired (eg. a binary
representation or digit grouping for hexadecimal representation).


__________________________________________________
Do You Yahoo!?
Talk to your friends online with Yahoo! Messenger.
http://im.yahoo.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk