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

Thread: int to CString

Hybrid View

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

  4. #4
    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.

  5. #5
    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.

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

    Re: int to CString

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

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

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

    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.

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

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

    Re: int to CString

    Beside that Siddhartha already said, what do you think?
    In the code below...
    Quote 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
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

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

    Re: int to CString

    ... Or he is just looking to reduce the number of "lines" in source code...
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

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

    Re: int to CString

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

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

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

    Do rate the posts you find useful.

  13. #13
    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.

  14. #14
    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.

  15. #15
    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.

Page 1 of 2 12 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