CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2009
    Posts
    21

    placing int into char*

    ok so i want to be able to take an integer break it up and place it into an char*
    example: int x = 10
    take 10 make it into '1' and '0' and then place those into char*

    now i have a method that does this and it looks like this..........buf is a char*
    Code:
          std::ostringstream sout;
          sout << x;
          std::string s(sout.str());
          length = s.length(); // Sets length equal to the length of the string
          buf = new char[length];
          const char *sz = s.c_str(); // Sets const char * equal to the string
          buf = (char*) sz; // Casts sz to char* from const char * and sets buf equal
    The problem is that iw ould rather now use the string class because unfortunatly im doin an assignment that is basically the string class. So any suggestions through them at me. Thank you

  2. #2
    Join Date
    Mar 2009
    Posts
    21

    Re: placing int into char*

    Ok so let me clarify..............

    sudo code:
    Code:
    int x = 10;
    char* buf = new char[ number of digits ];
    take x place into buf like '1' and '0'
    
     so that...
    
    buf[0] = 1
    buf[1] = 0

  3. #3
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: placing int into char*

    I just typed this up on the fly, so there's probably some errors:
    Code:
    int intPow(const int n, const int p) // Gets n to the power of p
    {
      int ret = n;
      for(int i = 1; i < p; i++)
        ret *= n;
      return ret;
    }
    int intLog10(const int n) // Gets log base 10
    {
      int ret = 0, num = n;
      while(num > 10)
      {
        num /= 10;
        ret++;
      }
      return ret;
    }
    char* intToStr (const int n) // Call this one with the number and it (maybe) will return it in string form
    {
      int strLen = intLog10 + 2, num = n, temp, temp2;
      char* ret = new char[strLen];
      for(int i = 0; i < strLen-1; i++)
      {
        char[i] = (temp2 = (num &#37; (temp = intPow(10,strLen-i-2)))) + '0';
        num -= temp * temp2;
      }
      ret[strLen-1] = '\0';
      return ret;
    }
    There's also itoa, of course:
    http://www.cplusplus.com/reference/c.../cstdlib/itoa/
    Last edited by Etherous; April 25th, 2009 at 07:52 PM.
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

  4. #4
    Join Date
    Mar 2009
    Posts
    21

    Re: placing int into char*

    very nice i will try it and get back her when i figure out what happens

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: placing int into char*

    Something worth considering if you use itoa or a similar function is the the number with the most digits that could *possibly* be held in an int is -2147483648. So if you pass a fixed-sized char array with at least 12 bytes to itoa, you know you'll have enough space.

  6. #6
    Join Date
    Mar 2009
    Posts
    21

    Re: placing int into char*

    just out of curiosity what is the #include for iota...... and isnt it a C# function
    correct me if im wrong

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: placing int into char*

    Quote Originally Posted by Lindley
    Something worth considering if you use itoa or a similar function is the the number with the most digits that could *possibly* be held in an int is -2147483648.
    Though strictly speaking an int could have an even larger range on a given implementation, but such a consideration is not so important for a school assignment.

    Quote Originally Posted by ductiletoaster
    just out of curiosity what is the #include for iota...... and isnt it a C# function
    correct me if im wrong
    It is not part of the C++ standard library, but if it is available you can probably #include <cstdlib> to use it.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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