-
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:01 AM
#3
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
#4
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
#5
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.
-
November 20th, 2005, 05:26 AM
#6
Re: int to CString
Originally Posted by cilu
Not a problem. _itoa is doing basically what I was doing, obviously my code was just something very simple as I indicated.
For that very reason - itoa should be used rather than re-inventing the wheel.
Originally Posted by cilu
That means you haven't written time critical software...
Having written many, I would like to let you know that what one thinks takes time really often doesn't in release mode. One doesn't know unless one profiles. Whether CString::Format takes time is a decision made well only after profiling it, and evaluating how much of the time is being spent in it on an application wide basis.
Single instances don't need optimization.
Clean code is desirable even for high performance application.
Originally Posted by cilu
Performance is sometime important.
"Sometime" is the keyword...
Last edited by Siddhartha; November 20th, 2005 at 05:30 AM.
-
November 20th, 2005, 05:29 AM
#7
Re: int to CString
ADSOFT, You can write your own function to display numeric values in a message box. You might name is 'MessageBoxInt' or something like that.
Practically, there is no way to avoid "conversion" to a string for displaying it - whatever you use: MessageBox, a static-control on dialog, or display text using DC.
-
November 20th, 2005, 05:30 AM
#8
Re: int to CString
Originally Posted by cilu
... Performance is sometime important.
...but more often maintenance is much more.
Anyhow the OP looks like a quiz, so relax guys.
-
November 20th, 2005, 05:35 AM
#9
Re: int to CString
Originally Posted by Sid
For that very reason - itoa should be used rather than re-inventing the wheel.
Exactly. I trully agree. I never was the advocate of wheel's reinvention.
-
November 20th, 2005, 05:38 AM
#10
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:44 AM
#11
Re: int to CString
Originally Posted by Ajay Vijay
ADSOFT, You can write your own function to display numeric values in a message box. You might name is 'MessageBoxInt' or something like that.
Practically, there is no way to avoid "conversion" to a string for displaying it - whatever you use: MessageBox, a static-control on dialog, or display text using DC.
Thanks
I just wanted to make sure you 'EXPERTS' didn't have a defacto trick you guys might want to share with me.
Anyhow, I a big fan of AfxMessageBox!!! .... I don't leave home without it
-
November 20th, 2005, 05:49 AM
#12
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
#13
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, 05:52 AM
#14
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:53 AM
#15
Re: int to CString
Let AfxMessageBox alone, if you need to convert from Integer types to CString directly, there are two options:
- Define a conversion routine globally.
- Derive a class from CString
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
|