473,237 Members | 1,171 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,237 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 5684
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
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.