Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Help] Converting in C++

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Odecey
Master Cheater
Reputation: 1

Joined: 19 Apr 2007
Posts: 259
Location: Scandinavia

PostPosted: Wed Jun 18, 2008 11:11 am    Post subject: [Help] Converting in C++ Reply with quote

How do i convert a char type to an int? Ive tried this:
Code:

char someChar[20] = "6";
int someInt = atoi(&someChar);

but it gives this error:
Code:

.\dllmain.cpp(633) : error C2664: 'atoi' : cannot convert parameter 1 from 'char (*)[20]' to 'const char *'
Back to top
View user's profile Send private message MSN Messenger
Symbol
I'm a spammer
Reputation: 0

Joined: 18 Apr 2007
Posts: 5094
Location: Israel.

PostPosted: Wed Jun 18, 2008 11:49 am    Post subject: Reply with quote

someChar is already a pointer to someChar[0], try:
Code:
atoi(&someChar[0]);

or
Code:
char* someChar = "6";
Back to top
View user's profile Send private message
Odecey
Master Cheater
Reputation: 1

Joined: 19 Apr 2007
Posts: 259
Location: Scandinavia

PostPosted: Wed Jun 18, 2008 12:11 pm    Post subject: Reply with quote

The second suggestion you gave still gives me the same error, and it also conflicts with other code in my program. Also wont the first suggestion only convert the first char in someChar?
Back to top
View user's profile Send private message MSN Messenger
Odecey
Master Cheater
Reputation: 1

Joined: 19 Apr 2007
Posts: 259
Location: Scandinavia

PostPosted: Wed Jun 18, 2008 12:41 pm    Post subject: Reply with quote

How come? There are several functions just for this in C#, why wouldnt they have implemented it for C++ aswell? (Talking about .ToString, int.parse, etc).
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren
Back to top
View user's profile Send private message MSN Messenger
lurc
Grandmaster Cheater Supreme
Reputation: 2

Joined: 13 Nov 2006
Posts: 1900

PostPosted: Wed Jun 18, 2008 1:52 pm    Post subject: Reply with quote

Code:
char aChar[2] = "6";
int nInt = atoi(aChar);


??

_________________
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 475

Joined: 09 May 2003
Posts: 25975
Location: The netherlands

PostPosted: Wed Jun 18, 2008 2:05 pm    Post subject: Reply with quote

Quote:

Code:

atoi(&someChar[0]);



Quote:

Code:

atoi(aChar);


Both are valid solutions, and do the same
atoi requires a pointer to the first character of the string and will parse it till the end (0 byte).
&someChar[0] will get from the first character in the array the address to it
This works, but isn't really required, since aChar on itself is also just an pointer that points to the first character of the array

_________________
Tools give you results. Knowledge gives you control.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
STN
I post too much
Reputation: 43

Joined: 09 Nov 2005
Posts: 2673

PostPosted: Thu Jun 19, 2008 4:49 am    Post subject: Re: [Help] Converting in C++ Reply with quote

Odecey wrote:
How do i convert a char type to an int? Ive tried this:
Code:

char someChar[20] = "6";
int someInt = atoi(&someChar);

but it gives this error:
Code:

.\dllmain.cpp(633) : error C2664: 'atoi' : cannot convert parameter 1 from 'char (*)[20]' to 'const char *'


i would point out the error.

Quote:
char someChar[20] = "6";
int someInt = atoi(&someChar);


the character marked with bold is the culprit. Like DB said, someChar already points to the first character, by using &(address of) operator, you are passing the address of address of someChar(or first character). Removing the & will solve the problem.

int someInt = atoi(someChar);


I hope this example will make it clear.
Code:
char someChar[20] = "6";
int someInt = atoi(someChar);
cout << "passing someChar: " << someChar << endl;
cout << "passing &someChar: "<< &someChar;


outputs

passing someChar: 6
passing &someChar: 0012FF6C

0012FF6C is a random memory address allocated for the string someChar, could be different on your pc.

You should read more about strings aka character arrays.
http://ee.hawaii.edu/~tep/EE160/Book/chap7/subsection2.1.1.2.html

_________________
Cheat Requests/Tables- Fearless Cheat Engine
https://fearlessrevolution.com
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites