LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-29-2005, 06:29 AM   #1
Diederick
LQ Newbie
 
Registered: Apr 2005
Posts: 25

Rep: Reputation: 15
cast from sting to int in Visual C++


Hi

I am wrking in Visual C++ (Visual Studio.NET 2003), and i am struggeling to case a String that I read from a text box to an integer (int). Can somone please help me with this.

Thanx D
 
Old 11-29-2005, 07:15 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Afaik the string has a method strtol (or something similar). Else there's the C-function strtol or strtoul.
 
Old 11-29-2005, 07:54 AM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
.Net has the following method
Code:
public: static short ToInt16(String* value);
depending on the type of integer you want there is also ToInt32 and ToInt64.

These are all documented on the MSDN siteConvert Class .

However if you want your code to be portable to another framework then you may want to look at the strtol() function, as previously mentioned, as this is part of the standard C library so #include <cstdlib>.

graeme.

Last edited by graemef; 11-29-2005 at 07:57 AM.
 
Old 11-29-2005, 08:27 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
A gentle request to please keep your questions in the forum

Quote:
When I type:

int var1 = txtTextBox->get_Text()->toInt32(###);

the ### wants an System::IFormatProvider. What is that and how can i use it to convert my string to an integer.
You probably want to change your code to:

Code:
int var1 = Convert::toInt32(txtTextBox->get_Text());
However a IFormatProvider is a class that will tell how you want the number formatted. That is will it include the sign, how about thousand separators, do you want a monetary symbol, etc.

The IFormatProvider is an optional parameter (if you use the static version of toInt32).

I hope that helps, an example of the code to set up an IFormatProvider is given at page to the C++ example

graeme
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Stupid Question: Microsoft Visual Studio 6.0 (A.K.A. Visual Basic 6) Will it run? S0RD3N Linux - Software 8 05-07-2008 08:42 PM
Help with Conversion from Sting to Char Diederick Programming 2 11-30-2005 08:00 AM
Sting.h alaios Programming 2 08-21-2005 04:35 AM
type cast from int to QString spoody_goon Programming 2 05-24-2005 06:14 AM
invalid types int[int] for array subscript scuzzman Programming 2 11-16-2004 09:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration