473,395 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Boost lexical_cast and hexa decimal format

Is it possible to make lexical_cast function to support stirngs wtih
'0x' format.

e.g
boost::lexical_cast<unsigned char>( "0x7E" )

This formatting throws bad_lexical_cast exception. Is there any way to
do that?
Jan 29 '08 #1
2 5691
Sarath <CS*****@gmail.comwrote in news:4c975578-3afe-4c83-a0a3-
68**********@v29g2000hsf.googlegroups.com:
Is it possible to make lexical_cast function to support stirngs wtih
'0x' format.

e.g
boost::lexical_cast<unsigned char>( "0x7E" )

This formatting throws bad_lexical_cast exception. Is there any way to
do that?
Boost would probably be the correct place to ask this question.
However, lexical_cast<is defined to have stream conversion semantics.
Sadly, streams don't understand the "0x" notation. That is,

std::istringstream in("0x7e");
int i;
in >i;

results in i have 0 as its value;

The second problem is that unsigned char is treated as a character and
not as an integral type. Thus:

std::istringstream in("17");
unsigned char c;
in >c;

results in c having a value of '1' or 49 if treated as an int. I am
sure that it could be done if they wanted to feed the string into strtol
() then convert it to a char, but does that make more sense than what
they do currently? It might for you, but for everyone?

The first step is probably convert to an unsigned short and then to an
unsigned char. That would at least get the conversion to a number going
rather than just grabbing the first character. I'm not real sure how
you would convince the stream lexical_cast<uses to convert from hex.
If you were using a real stream, you could do:

std::istringstream in("7f");
unsigned short us;
in >std::hex >us;
Like I said at the beginning, you might want to post to the boost group
to see if they have a good answer.

HTH,

joe
Jan 29 '08 #2
Sarath wrote:
Is it possible to make lexical_cast function to support stirngs wtih
'0x' format.
I believe that I have seen this question asked on the Boost list. Try
searching their archive. I believe that the answer is "no".

Phil.
Jan 29 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Adam | last post by:
Hello, I'm trying to decifer the data in the table that stores the data in the binary format. All numbers are placed in varbinary fields. All I know is the MS SQL 2000 database useing collation...
3
by: Golan | last post by:
Hello, I have a hexa file which I need to convert to decimal. I use memcpy into variables (char for one octet, short for 2 octets and int for 4 octets) and then print the value into the file by...
2
by: Nadav | last post by:
Hi, How can I use C# to print a number in hexadecimal e.g. printf(" %x "); or print 15 ( decimal ) as 'F' (hexa) Can a number be printed as binary as-well? Thanks in advance, Nadav.
2
by: sarchanakumar | last post by:
Hi friends, I want to write a program to add two hexa-decimal numbers. I know there is no way of direction addtion. My logic is, first convert two hexa-numbers into decimal after that we add and...
1
by: Chandra | last post by:
Hi, I have a problem related to File Reading. I have a file which contains data which is in Hexa format. I have to read that data and push it to a function which takes a parameter of type char....
1
by: Noah Roberts | last post by:
A while back I posted a question either here or to the boost user list about how to iterate through a vector of strings and perform a lexical cast on them into elements of a tuple. I was working...
6
by: hsmit.home | last post by:
Hello, I came across a strange error and it's really been bugging me. Maybe someone else has come across this and any insight would be appreciated. What I'm trying to accomplish is using...
3
by: neino | last post by:
Hello, did anyone of You have problems using boost::regex ? That code below : #include "boost/regex.hpp" /*1*/ int main() /*2*/ { /*3*/...
7
by: BubbaT | last post by:
What is the best way to pick up Boost? Thanks BubbaT
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.