CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 5 1234 ... LastLast
Results 1 to 15 of 61

Thread: int to CString

  1. #1
    Join Date
    Jun 2004
    Posts
    1,352

    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);

  2. #2
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: int to CString

    Quote 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...

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: int to CString

    Quote 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.

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: int to CString

    Quote Originally Posted by Sid
    It's too complicated Cilu...
    Did I say it's simple?

    Quote 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;
    }
    Quote 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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: int to CString

    Quote 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.

    Quote 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.

    Quote Originally Posted by cilu
    Performance is sometime important.
    "Sometime" is the keyword...
    Last edited by Siddhartha; November 20th, 2005 at 05:30 AM.

  7. #7
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    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.
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  8. #8
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: int to CString

    Quote 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.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  9. #9
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: int to CString

    Quote 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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  10. #10
    Join Date
    Jun 2004
    Posts
    1,352

    Re: int to CString

    Quote 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.

  11. #11
    Join Date
    Jun 2004
    Posts
    1,352

    Re: int to CString

    Quote 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

  12. #12
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: int to CString

    Quote 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?

    Quote 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.

  13. #13
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: int to CString

    Quote Originally Posted by ADSOFT
    Code:
    #Define Amessg  AfxMessageBox
    Why would you do something like that? I see no reason whatsoever for that.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  14. #14
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: int to CString

    Quote 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...

  15. #15
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    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
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

Page 1 of 5 1234 ... LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured