Lexical_cast and hex number
Hi! It is possibile to transform a string representing a hexadecimal number in the format "0xnumber" or "number" (without "0x") in an integer with lexical_cast<>? -- View this message in context: http://boost.2283326.n4.nabble.com/Lexical-cast-and-hex-number-tp3070247p307... Sent from the Boost - Users mailing list archive at Nabble.com.
Hi,
It is possibile to transform a string representing a hexadecimal number in the format "0xnumber" or "number" (without "0x") in an integer with lexical_cast<>?
In my experience, lexical_cast<> cannot transform a string representing a hexadecimal number into an integer. unsigned int x = lexical_cast("0x0badc0de"); // throw bad_lexical_cast Instead of using lexical_cast<>, you should use stringstream to do this kind of transform. For example, see the link below: http://stackoverflow.com/questions/1070497/c-convert-hex-string-to-signed-in... Regards, chenwj -- Wei-Ren Chen (陳韋任) Tel:886-2-2788-3799 #1667 Parallel Processing Lab, Institute of Information Science, Academia Sinica, Taiwan (R.O.C.)
Thanks! -- View this message in context: http://boost.2283326.n4.nabble.com/Lexical-cast-and-hex-number-tp3070247p307... Sent from the Boost - Users mailing list archive at Nabble.com.
Hi, On 2 December 2010 22:27, Claude <clros@tiscali.it> wrote:
Hi!
It is possibile to transform a string representing a hexadecimal number in the format "0xnumber" or "number" (without "0x") in an integer with lexical_cast<>?
There's another option I've been working on, construe_cast, which can be found here: https://github.com/VeXocide/construe_cast. It's a replacement for lexical_cast which I hope will make it into boost one day. The main difference is that whilst lexical_cast uses iostreams for its conversion, construe_cast is based off of boost.spirit. This means it's considerable faster at run-time and I've added support for tags some time ago, thus you can use: boost::construe_cast<unsigned int, boost::construe::tag::hex>("2c4") At the moment there is no support for "0x2c4" but that should be added later today (the tag::hex will then support "0x2c4", "0X2c4" as well as "2c4"). Documentation is still on the todo-list and something I hope to get to soon, but if you have any questions don't hesitate to ask them. Regards, Jeroen Habraken
Well, great work! It is possible have conversion for primitive types of <cstdint> (unt16_t, etc.) in the future? -- View this message in context: http://boost.2283326.n4.nabble.com/Lexical-cast-and-hex-number-tp3070247p307... Sent from the Boost - Users mailing list archive at Nabble.com.
On 3 December 2010 17:16, Claude <clros@tiscali.it> wrote:
Well, great work! It is possible have conversion for primitive types of <cstdint> (unt16_t, etc.) in the future?
They should already be supported (though possibly not in combination with the hex tag), do you have an example that is causing you problems? Regards, Jeroen
Yes...but I use the "hex" tag. Whit this line: construe_cast<uint16_t,construe::tag::hex>(myBuffer); I obtain this error: boost/construe/tag.hpp 120 error: static assertion failed: "sizeof(Target) == 0" -- View this message in context: http://boost.2283326.n4.nabble.com/Lexical-cast-and-hex-number-tp3070247p307... Sent from the Boost - Users mailing list archive at Nabble.com.
Hi, I've now added support for strings prefixed with an optional "0x" or "0X" to the tag::hex, which can be found in the master branch on github. More tags should be on the way. On 3 December 2010 22:19, Claude <clros@tiscali.it> wrote:
Yes...but I use the "hex" tag. Whit this line:
construe_cast<uint16_t,construe::tag::hex>(myBuffer);
I obtain this error:
boost/construe/tag.hpp 120 error: static assertion failed: "sizeof(Target) == 0"
You're right, and this should be resolved in a future version, I'll have to figure out how best to tackle it. Jeroen
Well! Thanks and..good work! -- View this message in context: http://boost.2283326.n4.nabble.com/Lexical-cast-and-hex-number-tp3070247p307... Sent from the Boost - Users mailing list archive at Nabble.com.
participants (3)
-
Claude
-
Jeroen Habraken
-
陳韋任