473,799 Members | 3,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Converting Strings

ok, i've got my file reader working just the way i need.
Thanks to everyones help, i can convert basic strings to cstrings. But,
i need a little more help.

Got a way of converting a string or cstring into a number? once i can do
this, i'll be done. I'm good at doing any thing else that i need to in a
c/c++ program. Just never have done file stuff before.

Again, thanks for any help you provide.

Daniel Moree
Sep 17 '05 #1
2 1763
Daniel Moree wrote:
ok, i've got my file reader working just the way i need.
Thanks to everyones help, i can convert basic strings to cstrings. But,
i need a little more help.

Got a way of converting a string or cstring into a number? once i can do
this, i'll be done. I'm good at doing any thing else that i need to in a
c/c++ program. Just never have done file stuff before.

Again, thanks for any help you provide.

Daniel Moree


Have a look at boost::lexical_ cast.

More down to earth: string-streams from <sstream> are your friend.

#include<sstrea m>

template< typename T >
T string_to_any ( const std::string & str ) {
T result;
std::stringstre am dummy ( str );
if ( !( dummy >> result ) ) {
throw( std::runtime_er ror( "conversion from string failed" ) );
}
return( result );
}

template< typename T >
std::string any_to_string ( const T & obj ) {
std::stringstre am dummy;
if ( !( dummy << obj ) ) {
throw( std::runtime_er ror( "conversion to string failed" ) );
}
return dummy.str();
}

Use like

double x = string_to_any< double >( "0.4" );

Best

Kai-Uwe Bux
Sep 17 '05 #2
On Fri, 16 Sep 2005 21:21:08 -0400, Daniel Moree wrote:
ok, i've got my file reader working just the way i need.
Thanks to everyones help, i can convert basic strings to cstrings. But,
i need a little more help.

Got a way of converting a string or cstring into a number? once i can do
this, i'll be done. I'm good at doing any thing else that i need to in a
c/c++ program. Just never have done file stuff before.

Again, thanks for any help you provide.

Daniel Moree


If by "number" you mean "integer", then check out atoi().

- Jay

Sep 17 '05 #3

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

Similar topics

4
10313
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with bytes that are not printable characters? I've tested this and it seems to work fine, but I want to make sure there isn't some condition or situation I'm not aware of that could cause problems. I'm doing this because it's easier to do some of my...
22
5498
by: Keith MacDonald | last post by:
Hello, Is there a portable (at least for VC.Net and g++) method to convert text between wchar_t and char, using the standard library? I may have missed something obvious, but the section on codecvt, in Josuttis' "The Standard C++ Library", did not help, and I'm still awaiting delivery of Langer's "Standard C++ IOStreams and Locales". Thanks,
7
2415
by: Jus! | last post by:
Hi. I am reading bits(1's & 0's) from a file and i wa wondering what is the most efficient method of converting these strings to individual int's? eg. File contains: 110001 010011 etc... Whats the best way to read in each line and break it up into individual ints?
7
4600
by: Paul K | last post by:
I'm writing a small component that needs to be as fast as possible. The component needs to convert a string to decimal during the course of it's processing. However, I need to test the string first to make sure it is numeric. Using the is keyword doesn't work (strings cannot be cast as decimal so false is always returned) and catching an exception from Convert.ToDecimal or decimal.Parse is too slow. Does anyone know of any methods...
2
5523
by: Fredrik Rodin | last post by:
All, I'm having problems with my resource manager in ASP.NET 2.0 after conversion from ASP.NET 1.1. Here is a background: In ASP.NET 1.1 All my user controls and aspx pages inherit from base classes. A base class includes this property (among others...;-)):
9
5679
by: Hugo Amselschlag | last post by:
Hi there, I've implemented a local system hook to suppress certain windows beeing displayed by the axWebbrowser control. Now I need some more information before I can decide, whether to suppress a window or not. My callback function get a long pointer (lParam) to a structure which contains further information. This structure is described at MSN as follows: typedef struct tagCREATESTRUCT {
5
4190
by: David | last post by:
I note that you can null teminate a string by adding controlchar.null. Is there a way of adding a null to a Buffer of Bytes and converting it to a string. I have packets coming in from a serial ports as bytes and some of these represent strings. (Like the Packed BCD date/time stamp etc). At present I read through each Byte and convert to char and append to a string.
2
4010
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The functions strtol, strtod, and strtoul, defined in <cstdlib>, convert a null- terminated character string to a long int, double, or unsigned long. You can use them to convert numeric strings of any base to a numeric
2
1595
by: HONOREDANCESTOR | last post by:
I've been converting a dotnet dll to a com object so that it can be called from vb6. If I want to pass a structure to a routine in the com object, like this: Call MyRoutine(byref MyStruct as MyStructType) it works when the structure is a simple structure full of integers, dates, etc. But if the structure contains one or more strings, I get an error message when compiling the caller. Apparently you cannot pass a structure that has...
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9078
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.