-
November 20th, 2005, 04:53 AM
#1
int to CString
Does anybody know of a faster to way (one step possibly) to convert an int to a CString?
ie.
int x
CString sout;
sout.Format("%i", x);
AfxMessageBox(sout);
I'm trying to eliminate the Format step, i.e,
sout.Format("%i", x);
-
November 20th, 2005, 04:56 AM
#2
Re: int to CString
Originally Posted by ADSOFT
Does anybody know of a faster to way (one step possibly) to convert an int to a CString?
The conversion code is 1 line at the moment... How much shorter do you want it to be... ?
Last edited by Siddhartha; November 20th, 2005 at 05:04 AM.
Reason: Quote added...
-
November 20th, 2005, 05:38 AM
#3
Re: int to CString
Originally Posted by Siddhartha
The conversion code is 1 line at the moment... How much shorter do you want it to be... ?
LOL!! ... thats a good one.
I was hoping for something along the lines of:
(CString)x;
where x is defined as an int
Actually (CString)x; compiles but it print out a box?
I want to do somthing like this
#Define CS CString
AfxMessageBox( "The Value for x is" + (CString)x + ",(CS) The Value for y is:", (CString)y);
I use AfxMessageBox for diagnostics all the time to monitor values at run-time.
I guess I could write my own "itoS" function and make it a global function
Code:
CString itoS ( int x) {
sout.Format("%i", x);
return sout;
}
I could then write something like this:
Code:
#Define Amessg AfxMessageBox
int x,y,z
x = some value in my program;
y = ...something else
z = ...
Amessg( "X is: " + itoS(x)
+ CS"\n Y is: " + itoS(y)
+ CS"\nZ is: " + itoS(z));
//... as a short hand way to print out diagnostics
.
When I have to monitor more that two variables the format function because too time consuming.
... what do you guys think .
Last edited by ADSOFT; November 20th, 2005 at 05:41 AM.
-
November 20th, 2005, 05:49 AM
#4
Re: int to CString
Originally Posted by ADSOFT
When I have to monitor more that two variables the format function because too time consuming.
Did you measure it?
Are you sure it is the CString::Format consuming a lot of time, and not any UI nuisance?
Originally Posted by ADSOFT
... what do you guys think .
This is OK -
strTest.Format (_T ("The Value for x is %i and The Value for y is: %i"), x, y);
Note that when you replace CString:Format by any other method, you are not saving on all the time that the Format function took, rather you are relying on saving a prospective Delta - which might even be negative (i.e. the replacement may consume even more time).
Like Ovidiu said, keeping code maintainable goes a long way towards less effort and efficient results.
-
November 20th, 2005, 05:52 AM
#5
Re: int to CString
Originally Posted by ADSOFT
Code:
#Define Amessg AfxMessageBox
Why would you do something like that? I see no reason whatsoever for that.
-
November 20th, 2005, 06:03 AM
#6
Re: int to CString
Originally Posted by cilu
Why would you do something like that? I see no reason whatsoever for that.
Because I hate typing out "AfxMessageBox",
I would then only type 4 letters.
of course you could use intellesense and first type
::AfxM..... i believe Intellesense homes in on AfxMessageBox after 3 or 4 letters. but then you have to erase the :: because I heard something about :: being a command to the compiler to call the old version of the function????
-
November 20th, 2005, 05:52 AM
#7
Re: int to CString
Originally Posted by ADSOFT
I guess I could write my own "itoS" function and make it a global function
Code:
CString itoS ( int x) {
sout.Format("%i", x);
return sout;
}
Ain't this slower than using CString::Format directly?
For one, you are creating a copy while returning it.
Believe in KISS: Keep it Simple S...
-
November 20th, 2005, 05:59 AM
#8
Re: int to CString
Originally Posted by ADSOFT
When I have to monitor more that two variables the format function because too time consuming.
... what do you guys think .
I bet any time on this one
Code:
CString strMsg;
strMsg.Format(_T("X is: %d\nY is: %d\nZ is: %d"), x, y, z);
AfxMessageBox(strMsg);
is more speedy and clearer than yours.
-
November 20th, 2005, 06:04 AM
#9
Re: int to CString
Beside that Siddhartha already said, what do you think?
In the code below...
Originally Posted by ADSOFT
Code:
Amessg( "X is: " + itoS(x)
+ CS"\n Y is: " + itoS(y)
+ CS"\nZ is: " + itoS(z));
... five calls of CString::operator+ do not consume time?
Last edited by ovidiucucu; November 20th, 2005 at 06:09 AM.
Reason: disable smiles
-
November 20th, 2005, 06:09 AM
#10
Re: int to CString
... Or he is just looking to reduce the number of "lines" in source code...
-
November 20th, 2005, 06:10 AM
#11
Re: int to CString
Originally Posted by ovidiucucu
I bet any time on this one
Code:
CString strMsg;
strMsg.Format(_T("X is: %d\nY is: %d\nZ is: %d"), x, y, z);
AfxMessageBox(strMsg);
is more speedy and clearer than yours.
You have to define a CString variable though.
With mine you jump right into AfxMessageBox
Code:
Amssg(
"First Var is: " + itoS(x)
+ (CS)"\nSecond Var is: " + itoS(y)
+ (CS)"\nThird Var is: " + itoS(z)
);
.. btw, I enjoyed your input
Last edited by ADSOFT; November 20th, 2005 at 06:14 AM.
-
November 20th, 2005, 06:17 AM
#12
Re: int to CString
Agreed. But there are some issues with your technique:
- Code readibility is not good.
- There are calls to 'itoS' many times, which will incur processing in pushing and popping function in call stack.
- itoS is using global object of type CString (which you think will save memory..)
- There would further be more function calls (CString:perator+)
-
November 20th, 2005, 05:01 AM
#13
Re: int to CString
Well, Format or sprintf are indeed time consuming functions, and if you call them very often it may become a problem. Well, you could try something like this, but I don't know how fast is comparing to Format.
Code:
int number = 1234;
int digits = (int)log10((double)number) + 1;
char text[16] = {0};
for(int i=0; i<digits; i++)
{
text[digits-1-i] = '0' + (number % 10);
number = number / 10;
}
Of course this piece of code works only with unsigned numbers. For negative numbers you have to take care of the sign too.
-
November 20th, 2005, 05:15 AM
#14
Re: int to CString
Originally Posted by cilu
Of course this piece of code works only with unsigned numbers. For negative numbers you have to take care of the sign too.
It's too complicated Cilu... Why not simply use itoa?
Code:
int nNum = 5;
char pszNum [32] = {0};
CString strTest (_itoa (nNum, pszNum, 10));
I believe people should not gain performance at the cost of readability.
For that reason, I see nothing wrong with CString::Format.
-
November 20th, 2005, 05:22 AM
#15
Re: int to CString
Originally Posted by Sid
It's too complicated Cilu...
Did I say it's simple?
Originally Posted by Sid
Why not simply use itoa?
Not a problem. _itoa is doing basically what I was doing, obviously my code was just something very simple as I indicated.
Code:
/* helper routine that does the main job. */
static void __cdecl xtoa (
unsigned long val,
char *buf,
unsigned radix,
int is_neg
)
{
char *p; /* pointer to traverse string */
char *firstdig; /* pointer to first digit */
char temp; /* temp char */
unsigned digval; /* value of digit */
p = buf;
if (is_neg) {
/* negative, so output '-' and negate */
*p++ = '-';
val = (unsigned long)(-(long)val);
}
firstdig = p; /* save pointer to first digit */
do {
digval = (unsigned) (val % radix);
val /= radix; /* get next digit */
/* convert to ascii and store */
if (digval > 9)
*p++ = (char) (digval - 10 + 'a'); /* a letter */
else
*p++ = (char) (digval + '0'); /* a digit */
} while (val > 0);
/* We now have the digit of the number in the buffer, but in reverse
order. Thus we reverse them now. */
*p-- = '\0'; /* terminate string; p points to last digit */
do {
temp = *p;
*p = *firstdig;
*firstdig = temp; /* swap *p and *firstdig */
--p;
++firstdig; /* advance to next two digits */
} while (firstdig < p); /* repeat until halfway */
}
/* Actual functions just call conversion helper with neg flag set correctly,
and return pointer to buffer. */
char * __cdecl _itoa (
int val,
char *buf,
int radix
)
{
if (radix == 10 && val < 0)
xtoa((unsigned long)val, buf, radix, 1);
else
xtoa((unsigned long)(unsigned int)val, buf, radix, 0);
return buf;
}
Originally Posted by Sid
I believe people should not gain performance at the cost of readability.
That means you haven't written time critical software... Performance is sometimes important.
Last edited by cilu; November 20th, 2005 at 05:33 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|