how to directly access to show a int variable as char * or string?
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);
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.
int nNumber = 9;char cMsg[64];sprintf(cMsg,"%d\0",nNumber); // <--- include string.h, or cstringMessageBox(NULL,cMsg,"Message Box Title",MB_OK|MB_ICONINFORMATION);
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!
hth
Jack
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
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"?
maybe I first convert the number to char number? I mean convert 40 to "40"?
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.
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'.
Quote:Original post by DigitalDelusionQuote: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'?
Quote:Original post by jollyjeffersQuote: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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement