LinuxQuestions.org
Review your favorite Linux distribution.
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-26-2001, 07:23 PM   #1
ReverseLogic
LQ Newbie
 
Registered: Aug 2001
Posts: 9

Rep: Reputation: 0
Question a question on type conversion in C++


yes, I am quite the newbie. My problem is, I'm trying to convert a CString Class (yes, this is in windows, I'm sorry but this is the only forum that i could think of) into a type int. thanks in advance.
-RL
 
Old 11-26-2001, 09:51 PM   #2
ReverseLogic
LQ Newbie
 
Registered: Aug 2001
Posts: 9

Original Poster
Rep: Reputation: 0
I Should also mention...

the conversion I want done. if the value of a CString is "2583", I want that converted to the interger 2583. thanks again.

-RL
 
Old 11-27-2001, 08:59 PM   #3
tyler_durden
Member
 
Registered: May 2001
Posts: 125

Rep: Reputation: 15
write a fucntion to do it yourself. first, get the string length.
then take the first char ie 2 convert that to a numer ie try printing out the char with the %d command and see what the offset is (the difference between ascii and the binary value, i think its 35). then subtract 35 from that char value. then multiply this time ten to the length power. get the next one do the same, but mult by ten to the length minus 1 power. add this to the first one and keep going
 
Old 11-27-2001, 11:06 PM   #4
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Character string to an integer use: atoi
Code:
char dude[] = "50";
dude = atoi( dude );
You will have to figure out how to get a CString into a character array. The STL uses a .c_str() member funciton.

Int to a string use: sprintf
Code:
char dude[50];
int num = 50;
sprintf( dude, "%d", num );
This is quick and dirty C code. Many may say that sprintf nor atoi is safe to use. Here is a C++ replacement for using sprintf.

Code:
#include <strstream>
#include <iostream>

main()
{
   std::strstream dude;
   int i = 50;
   dude << i << ends;
   cout << dude.str() << endl;
}
 
  


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
Video Conversion Question Agentvenom Linux - Software 6 10-07-2005 02:19 AM
Data type conversion in C zaichik Programming 6 09-10-2005 05:47 PM
c + type conversion dilberim82 Programming 8 03-08-2005 12:17 PM
Video conversion question. PLEASE HELP gbrad Linux - Software 4 04-19-2003 08:47 AM
conversion to non-const reference type max2878 Linux - General 2 05-23-2002 01:25 PM

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

All times are GMT -5. The time now is 08:45 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