
I'd like to do something like: boost::uint16_t b(std::lexical_castboost::uint16_t("xa")); I have no control of the string stream that lexical_cast uses internally, so how could I implement this? P.S.: There are a lot of false statements made about this functionality on the web, some people claim that boost::lexical_cast<> can do the conversion out of the box, but this is not so in my tests.

anony wrote:
I'd like to do something like:
boost::uint16_t b(std::lexical_castboost::uint16_t("xa"));
I have no control of the string stream that lexical_cast uses internally, so how could I implement this?
P.S.: There are a lot of false statements made about this functionality on the web, some people claim that boost::lexical_cast<> can do the conversion out of the box, but this is not so in my tests.
_______________________________________________
Long ago I made this change in a vendor drop of boost. It worked well
for me then; however, I have replaced all of my string conversions with
qi/karma semantics since.
I personally think this should be the default behavior in lexical_cast.
The diff is from 1.37.0 IIRC but I don't think lexical_cast changes much.
hth
michael
Index: trunk/firmware/vendor/boost/boost/lexical_cast.hpp
===================================================================
--- trunk/firmware/vendor/boost/boost/lexical_cast.hpp (revision 582)
+++ trunk/firmware/vendor/boost/boost/lexical_cast.hpp (revision 632)
@@ -571,4 +571,5 @@
{
stream.unsetf(std::ios::skipws);
+ stream.setf( std::ios::fmtflags(), std::ios::basefield );
lcast_set_precision(stream, (Source*)0, (Target*)0);
}
@@ -758,4 +759,5 @@
std::basic_istream<CharT> stream(static_cast

I have replaced all of my string conversions with qi/karma semantics since. ---------------------------------- Michael Caisse
What are "qi/karha semantics"? --John TradeStation Group, Inc. is a publicly-traded holding company (NASDAQ GS: TRAD) of three operating subsidiaries, TradeStation Securities, Inc. (Member NYSE, FINRA, SIPC and NFA), TradeStation Technologies, Inc., a trading software and subscription company, and TradeStation Europe Limited, a United Kingdom, FSA-authorized introducing brokerage firm. None of these companies provides trading or investment advice, recommendations or endorsements of any kind. The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer.

John Dlugosz wrote:
I have replaced all of my string conversions with qi/karma semantics since. ---------------------------------- Michael Caisse
What are "qi/karha semantics"?
--John
Sorry. That was very poorly worded. I use qi and karma (boost.spirit) to do most conversions now. It is blazing fast and I don't have to keep patching lexical_cast to handle bases other than base 10. There has been some discussion on the spirit mailing list by Jeroen Habraken submitting a spirit_cast that will look a lot like lexical_cast syntax. Initial tests are very very promising and I think the code looks clean. I hope he submits it soon. michael -- ---------------------------------- Michael Caisse Object Modeling Designs www.objectmodelingdesigns.com

-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of anony Sent: Thursday, July 15, 2010 1:09 AM To: boost-users@lists.boost.org Subject: [Boost-users] boost::lexical_cast<> with hex strings? I'd like to do something like: boost::uint16_t b(std::lexical_castboost::uint16_t("xa")); I have no control of the string stream that lexical_cast uses internally, so how could I implement this? P.S.: There are a lot of false statements made about this functionality on the web, some people claim that boost::lexical_cast<> can do the conversion out of the box, but this is not so in my tests. What about doing this struct hexint { operator unsigned int() { return value; } unsigned int value; }; inline std::istream &operator>>(std::istream &in, hexint &rhs) { std::ios::fmtflags flags=in.flags(); in.unsetf(std::ios::oct); in.unsetf(std::ios::dec); in.unsetf(std::ios::hex); unsigned int temp=0; in >> temp; in.setf(flags); if(in.bad()) { throw boost::bad_lexical_cast(); } rhs.value=temp; return in; } //Then you can do hexint x=boost::lexical_cast<hexint>("0x2a");
participants (4)
-
anony
-
boostļ¼ bluedreamer.com
-
John Dlugosz
-
Michael Caisse