472,354 Members | 1,126 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

istringstream "hexadecimal string" value

The string object value_f doesn't produce the right output. At issue,
- I suspect - is the conversion from string to int with istringstream.
An alternate approach? Thanks in advance

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

// Conversion Functions
template<typename T>
T fromString(const std::string& s) {
std::istringstream is(s);
T t;
is >> t;
return t;
}
template<typename T>
std::string toString(const T& t) {
std::ostringstream s;
s << t;
return s.str();
}
//
// test function to convert values:
// 0xAFFF from string to int
// 0xAFFF from int to string

int main()
{

int d(0xAFFF);
string value_d = toString( d );
cout << "value_d is [" << value_d << "]" << endl;

string value_e( " 4999 " );
string value_f( " 0xAFFF " );

int val_1 = fromString<int>( value_e );
cout << "val_1 is [" << val_1 << "]" << endl;
int val_2 = fromString<int>( value_f );
cout << "val_2 is [" << val_2 << "]" << std::hex << endl;

}
Jul 22 '05 #1
7 3549
"ma740988" <ma******@pegasus.cc.ucf.edu> wrote...
The string object value_f doesn't produce the right output. At issue,
- I suspect - is the conversion from string to int with istringstream.
An alternate approach?
The default extraction operator with default format setting expects
a _decimal_ notation.
Thanks in advance

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

// Conversion Functions
template<typename T>
T fromString(const std::string& s) {
Change to

T fromString(const std::string& s,
std::ios_base& (*f)(std::ios_base&) = std::dec) {

std::istringstream is(s);
T t;
is >> t;
Change to

is >> f >> t;
return t;
}
template<typename T>
std::string toString(const T& t) {
Change to

std::string toString(const T& t,
std::ios_base& (*f)(std::ios_base&) = std::dec) {
std::ostringstream s;
s << t;
Change to

s << f << t;
return s.str();
}
//
// test function to convert values:
// 0xAFFF from string to int
// 0xAFFF from int to string

int main()
{

int d(0xAFFF);
string value_d = toString( d );
cout << "value_d is [" << value_d << "]" << endl;

string value_e( " 4999 " );
string value_f( " 0xAFFF " );

int val_1 = fromString<int>( value_e );
cout << "val_1 is [" << val_1 << "]" << endl;
int val_2 = fromString<int>( value_f );
Change to

int val_e = fromString<int>( value_f, hex );
cout << "val_2 is [" << val_2 << "]" << std::hex << endl;

}


HTH

V
Jul 22 '05 #2
"Victor Bazarov" <v.********@comAcast.net> wrote in message news:<ZSDfd.253989$wV.7201@attbi_s54>...
"ma740988" <ma******@pegasus.cc.ucf.edu> wrote...

int main()
{

int d(0xAFFF);
string value_d = toString( d );
cout << "value_d is [" << value_d << "]" << endl;

string value_e( " 4999 " );
string value_f( " 0xAFFF " );

int val_1 = fromString<int>( value_e );
cout << "val_1 is [" << val_1 << "]" << endl;
int val_2 = fromString<int>( value_f );


Change to

int val_e = fromString<int>( value_f, hex );
cout << "val_2 is [" << val_2 << "]" << std::hex << endl;

}


HTH

V

Beautiful. One question though (Haven't gotten through Langer/Kreft)
oct doesn't co-operate

template<typename T>
T fromString(const std::string& s,
std::ios_base& (*f)(std::ios_base&) = std::dec)
{
std::istringstream is(s);
T t;
is >> f >> t;
return t;
}

string value_e( " 4999 " );
int val_e = fromString<int>( value_e, oct);
The output is 4.
Jul 22 '05 #3
ma740988 wrote:
[..] One question though (Haven't gotten through Langer/Kreft)
oct doesn't co-operate

template<typename T>
T fromString(const std::string& s,
std::ios_base& (*f)(std::ios_base&) = std::dec)
{
std::istringstream is(s);
T t;
is >> f >> t;
return t;
}

string value_e( " 4999 " );
int val_e = fromString<int>( value_e, oct);
The output is 4.


'9' is not a valid input for the octal converter, is it?

V
Jul 22 '05 #4
Victor Bazarov <v.********@comAcast.net> wrote in message news:<i5***************@newsread1.dllstx09.us.to.v erio.net>...
ma740988 wrote:
[..] One question though (Haven't gotten through Langer/Kreft)
oct doesn't co-operate

template<typename T>
T fromString(const std::string& s,
std::ios_base& (*f)(std::ios_base&) = std::dec)
{
std::istringstream is(s);
T t;
is >> f >> t;
return t;
}

string value_e( " 4999 " );
int val_e = fromString<int>( value_e, oct);
The output is 4.


'9' is not a valid input for the octal converter, is it?

V


Ah!!! Being 'wrapped up in the stream' , I overlooked that, however it
gets interesting. First, I'm assuming that the stream modifier
std::oct is what you're referring to as the octal converter? Now i
need to grab my text for a refresher but I conclude - based on your
reply - that the conversion is predicated upon passing single digit to
the converter. IOW 4 is passed to the converter, then 9 and so on.

Now, heres a case where the 'octal converter/stream modifier(?)'
produced the desired output which refutes my prior conclusion.

int idx(4999);
cout << oct << idx << endl;
Jul 22 '05 #5
ma740988 wrote:
Victor Bazarov <v.********@comAcast.net> wrote in message news:<i5***************@newsread1.dllstx09.us.to.v erio.net>...
ma740988 wrote:
[..] One question though (Haven't gotten through Langer/Kreft)
oct doesn't co-operate

template<typename T>
T fromString(const std::string& s,
std::ios_base& (*f)(std::ios_base&) = std::dec)
{
std::istringstream is(s);
T t;
is >> f >> t;
return t;
}

string value_e( " 4999 " );
int val_e = fromString<int>( value_e, oct);
The output is 4.


'9' is not a valid input for the octal converter, is it?

V

Ah!!! Being 'wrapped up in the stream' , I overlooked that, however it
gets interesting. First, I'm assuming that the stream modifier
std::oct is what you're referring to as the octal converter? Now i
need to grab my text for a refresher but I conclude - based on your
reply - that the conversion is predicated upon passing single digit to
the converter. IOW 4 is passed to the converter, then 9 and so on.

Now, heres a case where the 'octal converter/stream modifier(?)'
produced the desired output which refutes my prior conclusion.

int idx(4999);
cout << oct << idx << endl;


'oct', like 'dec' or 'hex', control how the string is converted to and
from the internal representation. Those format flags are bi-directional.

You can think of it that they turn on certain converters in the stream.
Once you say 'oct', the next string is interpreted as octal if you input
or the next number is output in the octal form if you output. Converters
are bi-directional too.

V
Jul 22 '05 #6
Victor Bazarov <v.********@comAcast.net> wrote in message news:<i5Ofd.7225

I'm using google as a newsreader (too cheap right now) and as a result
I dont see your responses momentarily.
In any event, after seeing your use of the fuction pointer that
returns a reference to ios_base. I went back and retrofitted some
'old' code to do the same.
One things that's puzzled me in the past (string related) is the
results of the program below. The output is 0 when i expect 0x00.
0xFF, etc. works fine.
//PARAMETERIZED OVER CHARACTER AND NUMERIC TYPE TO BE CONVERTED
template <typename CT, typename T>
std::basic_string<CT> convertToStr(const T& t, std::ios_base &
(*f)(std::ios_base&), const std::streamsize precision)
{
std::basic_ostringstream<CT> oss;
oss.setf(std::ios::showbase);
oss << std::fixed << std::setprecision(precision)<< f << t;
return oss.str();
};

int main()
{
cout << convertToStr<char>(0x00, std::hex, 0) << endl;
}
Jul 22 '05 #7
ma740988 wrote:
Victor Bazarov <v.********@comAcast.net> wrote in message news:<i5Ofd.7225

I'm using google as a newsreader (too cheap right now) and as a result
I dont see your responses momentarily.
In any event, after seeing your use of the fuction pointer that
returns a reference to ios_base. I went back and retrofitted some
'old' code to do the same.
One things that's puzzled me in the past (string related) is the
results of the program below. The output is 0 when i expect 0x00.
0xFF, etc. works fine.
I am not surprised. The conversion to hex is done precisely as if it
were done by 'printf'. Since 'printf' never prepends 0 with 0x, neither
does the ostream. The C Standard says that only a non-zero value will
be prefixed with 0x. If you want _all_ values to be prefixed, you should
(a) drop the 'showbase' and (b) add "0x" for the hex.


//PARAMETERIZED OVER CHARACTER AND NUMERIC TYPE TO BE CONVERTED
template <typename CT, typename T>
std::basic_string<CT> convertToStr(const T& t, std::ios_base &
(*f)(std::ios_base&), const std::streamsize precision)
{
std::basic_ostringstream<CT> oss;
oss.setf(std::ios::showbase);
oss << std::fixed << std::setprecision(precision)<< f << t;
return oss.str();
};

int main()
{
cout << convertToStr<char>(0x00, std::hex, 0) << endl;
}

V
Jul 22 '05 #8

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

Similar topics

7
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex'...
8
by: johnsocs | last post by:
How do you validate the following XML document, I'm having problems with element 'one' with the attribute xsi:type="xsd:string" <?xml version="1.0" encoding="UTF-8"?> <zero...
1
by: Kashish | last post by:
Is file<<"Some string"<<endl is an atomic operation. where file is an ofstream object. If we output a string in ofstream and we provide endl after that,Can we make sure the whole string will be...
0
by: Andrew Burgher | last post by:
Feeding the following .xsd into the XsdObjectGen (v1.4.2.0) tool produces an invalid attribute: with DataType="System.String". Has anybody seen this behaviour before? Is this a bug in...
3
by: Andy Sutorius via DotNetMonster.com | last post by:
Hi, I have a Sorted List that has 9 key/value pairs. I am trying to take one of the key/value pairs and store the value of that key into a string variable. However, the value that actually gets...
17
by: Petyr David | last post by:
Just looking for the simplest. right now my perl script returns an error messge to the user if the date string is invalid. would like to do this before accessing the server. TX
1
by: Ducknut | last post by:
Not so much a problem as a discussion. I am currently in the early stages of designing a database to hold a bunch of water quality data (e.g., concentrations of heavy metals in drinking water). Water...
10
by: R2d2Rabeau | last post by:
Hi, I'm new to C#, please be patient :-) I am trying to pass an Arraylist (myAL) to a jagged array (myAL2) . The problem I have is that when I write the values to a file i do not get the values...
1
by: mato81 | last post by:
Hi all! I am a newbie to WSDL. I have a questions which has been driving me crazy... If I would have a WSDL with a types element somewhat like below, what is the point of the third last row...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...

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.