Ricky65
2011-08-09 01:47:42 UTC
C++0x provides the useful std::to_string/to_wstring functions for
converting numeric values to strings. However, I was disappointed to
learn that users can't specify the base of the conversion; at the
moment the std::to_string/to_wstring functions only convert a numeric
to a base ten string. I think this is an oversight and find this very
limiting.
In contrast , the similar non-standard but widespread C function itoa
allows the user to specify the base of the conversion. e.g.
hexadecimal, octal and binary. For example:
int x = 23;
char buffer [32];
itoa(x, buffer, 2) //represent the value as a string in base 2
(binary)
I find it rather embarrassing that a C function, albeit non-standard,
has more functionality than these C++ functions.
I understand that the overloaded std::to_string/to_wstring functions
call sprintf and wsprintf. The problem with this approach is that
sprintf can only convert to decimal, hexadecimal and octal bases. Why
do these functions have to use sprintf? I don't know why it couldn't
have been left to implementers to write their own versions of these
functions without these limitations.
It would be preferable if the user could select the base of the
numeric conversion.
The solution is simple, provide an extra argument for the base with a
default argument of 10. For example:
std::string to_string(int val, int base = 10);
This would be backward compatible with the current functions as it
would default to base ten (decimal) conversion as the functions
currently do whilst at the same time allowing the user to specify the
base of the numeric conversion.
If something like this doesn't get implemented, I suspect people will
use itoa or its variants, or roll their own similar function, reducing
the utility of the std::to_string/to_wstring functions.
Kind regards
Riccardo Marcangelo
converting numeric values to strings. However, I was disappointed to
learn that users can't specify the base of the conversion; at the
moment the std::to_string/to_wstring functions only convert a numeric
to a base ten string. I think this is an oversight and find this very
limiting.
In contrast , the similar non-standard but widespread C function itoa
allows the user to specify the base of the conversion. e.g.
hexadecimal, octal and binary. For example:
int x = 23;
char buffer [32];
itoa(x, buffer, 2) //represent the value as a string in base 2
(binary)
I find it rather embarrassing that a C function, albeit non-standard,
has more functionality than these C++ functions.
I understand that the overloaded std::to_string/to_wstring functions
call sprintf and wsprintf. The problem with this approach is that
sprintf can only convert to decimal, hexadecimal and octal bases. Why
do these functions have to use sprintf? I don't know why it couldn't
have been left to implementers to write their own versions of these
functions without these limitations.
It would be preferable if the user could select the base of the
numeric conversion.
The solution is simple, provide an extra argument for the base with a
default argument of 10. For example:
std::string to_string(int val, int base = 10);
This would be backward compatible with the current functions as it
would default to base ten (decimal) conversion as the functions
currently do whilst at the same time allowing the user to specify the
base of the numeric conversion.
If something like this doesn't get implemented, I suspect people will
use itoa or its variants, or roll their own similar function, reducing
the utility of the std::to_string/to_wstring functions.
Kind regards
Riccardo Marcangelo
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]