int to char

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • wak

    int to char

    hi!
    how can I convert an int to char !?

    string foo = "Hello";
    for (int i=0 ; i <10 ; i++)
    cout << foo.append(i);




    --
    Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
  • Peter van Merkerk

    #2
    Re: int to char

    > hi![color=blue]
    > how can I convert an int to char !?[/color]

    I guess you intended to ask "how can I convert an int to a string"
    [color=blue]
    > string foo = "Hello";
    > for (int i=0 ; i <10 ; i++)
    > cout << foo.append(i);[/color]



    --
    Peter van Merkerk
    peter.van.merke rk(at)dse.nl


    Comment

    • Christoph Rabel

      #3
      Re: int to char

      wak wrote:[color=blue]
      > hi!
      > how can I convert an int to char !?[/color]

      You can use a stringstream or sprintf.
      [color=blue]
      > string foo = "Hello";
      > for (int i=0 ; i <10 ; i++)
      > cout << foo.append(i);[/color]

      cout << foo << i;

      should work in this case. (It doesnt the same what you have written but
      probably what you want)

      In case you really want the above behaviour, you could, in this example
      do it the following way:

      for (int i=0 ; i <10 ; i++)
      {
      foo += '0' + i;
      cout << foo;
      }

      Please be advised, that it only works with ints in the range 0-9 and
      that C++98 does not guarantee that the letters 0-9 are in this order.

      But C guarantees it and the C++2003 standard. (Please correct me if Im
      wrong in this) So it should be fairly safe to use.

      hth

      Christoph

      Comment

      • Jeremy Cowles

        #4
        Re: int to char


        "Christoph Rabel" <odie@hal9000.v c-graz.ac.at> wrote in message
        news:3fd5a06a$0 $18044$3b214f66 @aconews.univie .ac.at...[color=blue]
        > wak wrote:[color=green]
        > > hi!
        > > how can I convert an int to char !?[/color]
        >
        > You can use a stringstream or sprintf.[/color]

        So C++ has no simple way to concat a string and other variables? For
        example, in other languages (using C++ syntax):

        //--

        String s("my string is");
        String n("long.");
        int i = 99;

        s += i + n;
        s += i.ToString( ) + n;
        s += ((string) i ) + n;

        //--

        You can't do that in C++? I am reading a C++ book, and I was wondering why
        the author never described how to concat vars (aside from the insertion
        operator)... That seems weird.

        TIA,
        Jeremy

        Comment

        • Chris \( Val \)

          #5
          Re: int to char


          "wak" <wak@libero.i t> wrote in message
          news:63d5a65faa 0876a048aead8a1 c90d7c3.71413@m ygate.mailgate. org...
          | hi!
          | how can I convert an int to char !?
          |
          | string foo = "Hello";
          | for (int i=0 ; i <10 ; i++)
          | cout << foo.append(i);

          static_cast<cha r>( i )

          Cheers.
          Chris Val


          Comment

          • Karl Heinz Buchegger

            #6
            Re: int to char

            Jeremy Cowles wrote:[color=blue]
            >
            > "Christoph Rabel" <odie@hal9000.v c-graz.ac.at> wrote in message
            > news:3fd5a06a$0 $18044$3b214f66 @aconews.univie .ac.at...[color=green]
            > > wak wrote:[color=darkred]
            > > > hi!
            > > > how can I convert an int to char !?[/color]
            > >
            > > You can use a stringstream or sprintf.[/color]
            >
            > So C++ has no simple way to concat a string and other variables? For
            > example, in other languages (using C++ syntax):
            >
            > //--
            >
            > String s("my string is");
            > String n("long.");
            > int i = 99;
            >
            > s += i + n;
            > s += i.ToString( ) + n;
            > s += ((string) i ) + n;
            >
            > //--
            >
            > You can't do that in C++?[/color]

            You can do everything in C++.
            It's just that some things don't come 'out of the box'.
            For some things you need to do a little bit of work by yourself.
            But once you have done that you have that functionality:

            Taken from the FAQ and modified a little bit

            inline std::string stringify(int x)
            {
            std::ostringstr eam o;
            if (!(o << x))
            throw BadConversion(" stringify(doubl e)");
            return o.str();
            }

            can be used as in:

            std::string s( "my string is" );
            std::string n( "long." );
            int i = 99;

            s += stringify( i ) + n;

            You might also go to http://www.boost.org and look up lexical_cast.
            Basically it does the very same: convert whatever you give to it into
            a string. Whatever you do with that string is then up to you.
            [color=blue]
            > I am reading a C++ book, and I was wondering why
            > the author never described how to concat vars (aside from the insertion
            > operator)... That seems weird.[/color]

            It's very simple: First convert everything to a std::string, then concat
            all the individual pieces.

            --
            Karl Heinz Buchegger
            kbuchegg@gascad .at

            Comment

            • jeffc

              #7
              Re: int to char


              "Jeremy Cowles" <jeremy.cowle s[nosp@m]asifl.com> wrote in message
              news:5ikBb.3420 1$b01.831950@tw ister.tampabay. rr.com...[color=blue]
              >
              > "Christoph Rabel" <odie@hal9000.v c-graz.ac.at> wrote in message
              > news:3fd5a06a$0 $18044$3b214f66 @aconews.univie .ac.at...[color=green]
              > > wak wrote:[color=darkred]
              > > > hi!
              > > > how can I convert an int to char !?[/color]
              > >
              > > You can use a stringstream or sprintf.[/color]
              >
              > So C++ has no simple way to concat a string and other variables? For
              > example, in other languages (using C++ syntax):
              >
              > //--
              >
              > String s("my string is");
              > String n("long.");
              > int i = 99;
              >
              > s += i + n;
              > s += i.ToString( ) + n;
              > s += ((string) i ) + n;
              >
              > //--
              >
              > You can't do that in C++?[/color]

              You can't do that in C++ because you're using "int", which is a built in
              type from the C days. It's not a class. You could easily write one of
              course, but your code wouldn't be portable.


              Comment

              • jeffc

                #8
                Re: int to char


                "Chris ( Val )" <chrisval@bigpo nd.com.au> wrote in message
                news:br4jju$287 1kd$1@ID-110726.news.uni-berlin.de...[color=blue]
                >
                > "wak" <wak@libero.i t> wrote in message
                > news:63d5a65faa 0876a048aead8a1 c90d7c3.71413@m ygate.mailgate. org...
                > | hi!
                > | how can I convert an int to char !?
                > |
                > | string foo = "Hello";
                > | for (int i=0 ; i <10 ; i++)
                > | cout << foo.append(i);
                >
                > static_cast<cha r>( i )[/color]

                That wasn't very nice.


                Comment

                Working...