Jump to content
  • Advertisement
Sign in to follow this  
derek7

how to directly access to show a int variable as char * or string?

This topic is 4721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I do this ; but no work. int i=9; char *c; c = (char*)&i; c = c+1; *c = 0; c = c-1; MessageBox(0,c,0,0);

Share this post


Link to post
Share on other sites
Advertisement
An int is not a string so simply casting will not work. You can use the search function and find out about itoa() and sprintf(). If you look those up you will find out how they work.

Share this post


Link to post
Share on other sites
int  nNumber = 9;
char cMsg[64];

sprintf(cMsg,"%d\0",nNumber); // <--- include string.h, or cstring
MessageBox(NULL,cMsg,"Message Box Title",MB_OK|MB_ICONINFORMATION);

Share this post


Link to post
Share on other sites
You could also look into using <sstream> if memory serves me right... std::ostringstream and std::istringstream are the ones you want to look at. You can use these to create a templated function that will, for most cases, work like a "toString" or "fromString" - very useful for debug output [smile]

I'm hoping I haven't completely cocked this next bit up as it's straight off the top of my head. I'm at work right now so don't have access to my utility files that I have these in!

template< class T >
std::string toString( const T& value )
{
std::ostringstream oss;
oss << value;
return oss.str();
}

template< class T >
T fromString( const std::string& text )
{
std::istringstream iss( text );
T ret;
iss >> ret;
return ret;
}

std::string IntAsText = toString< int >( 1234 );
int TextAsInt = fromString< int >( IntAsText );



hth
Jack

Share this post


Link to post
Share on other sites
I just want to directly access memory through a pointer.


maybe I first convert the number to char number? I mean convert 40 to "40"?



Share this post


Link to post
Share on other sites
Quote:
Original post by derek7
I just want to directly access memory through a pointer.

so do this way not possible?


Since the datatypes are conceptually diffrent it just won't work you'll have to convert it somehow.

Share this post


Link to post
Share on other sites
Quote:
Original post by derek7
I just want to directly access memory through a pointer.

It might well help if you give people a bit more explanation as to what you're trying to achieve - there might well be a better/easier way. Your original example is a little unclear [smile]


int i=9;
char *c;
c = (char*)&i; //So you want C="9" ??
c = c+1; //do you want C="10" now?
*c = 0; //C="0"?
c = c-1; //C="-1" now?


Are my comments showing what you're trying to do?

Quote:
Original post by derek7
maybe I first convert the number to char number? I mean convert 40 to "40"?

Bare in mind that your "char number" is just a human readable representation, but the most part your CPU will not operate on "40" as a number - rather two characters - '4' and '0'.

Share this post


Link to post
Share on other sites
Quote:
Original post by DigitalDelusion
Quote:
Original post by derek7
I just want to directly access memory through a pointer.

so do this way not possible?


Since the datatypes are conceptually diffrent it just won't work you'll have to convert it somehow.


so how to do?

I think I need convert character to ascii code.

like: if ( int i = 97) then return 'a'?

Share this post


Link to post
Share on other sites
Quote:
Original post by jollyjeffers
Quote:
Original post by derek7
I just want to directly access memory through a pointer.

It might well help if you give people a bit more explanation as to what you're trying to achieve - there might well be a better/easier way. Your original example is a little unclear [smile]


int i=9;
char *c;
c = (char*)&i; //So you want C="9" ??
c = c+1; //do you want C="10" now?
*c = 0; //C="0"?
c = c-1; //C="-1" now?


Are my comments showing what you're trying to do?

Quote:
Original post by derek7
maybe I first convert the number to char number? I mean convert 40 to "40"?

Bare in mind that your "char number" is just a human readable representation, but the most part your CPU will not operate on "40" as a number - rather two characters - '4' and '0'.



I mean first in memory have a int i = 97;
so I just show the 97 (maybe now is character) on screen.
that is it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!