CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: int array..

  1. #1
    Join Date
    Sep 2007
    Posts
    85

    int array..

    hi friends..

    i have an array same this

    int array[] = {225, 335};
    int codeguru[2];

    i would to insert every bite of array[0]
    in codeguru array...

    if i do
    strcpy , show me an error becouse the imput is
    a char* not a int..

    how i can??..

    output of variables..
    codeguru[0] = 2
    codeguru[1] = 2
    codeguru[2] = 5


    thanks!!

  2. #2
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: int array..

    See memcpy

    EDIT : sorry, you need to convert the input int array element into string (see FAQs for ways to do that) and take each character and store into the second int array. Another way could be to take out each digits (units/tens/hundreds etc places) out of the int directly and assign as needed into the int array.
    Last edited by exterminator; October 18th, 2007 at 11:32 AM.

  3. #3
    Join Date
    Sep 2007
    Posts
    85

    Re: int array..

    ohh thanks a lot..
    whith your help i progress to define the array in string..
    but i don't know how now i convert the string in an int array..

    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    
    using namespace std;
    
    template <class T>
    std::string to_string(T t, std::ios_base & (*f)(std::ios_base&))
    {
      std::ostringstream oss;
      oss << f << t;
      return oss.str();
    }
    
    int main(int argc, char *argv[])
    { 
        
    
       int array[] = {112, 113};     
            string s1;
        int numeri;
        
       s1 = to_string<int>(array[0], dec);
       
       cout << s1; 
       return 0;
    
    }
    thanks!

  4. #4
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645

    Re: int array..

    Here's a cheap, bad, hack
    Code:
    memcpy((char*)array, (char*)codeguru, 2*sizeof(int))
    Why do you want to refer to the data in bytes? Both are ints already.
    No need to change to a different data type.

    Code:
    for( int i=0; i<2; i++)
        codeguru[i] = array[i]

  5. #5
    Join Date
    Sep 2007
    Posts
    85

    Re: int array..

    Quote Originally Posted by cvogt61457
    Why do you want to refer to the data in bytes? Both are ints already.
    No need to change to a different data type.

    Code:
    for( int i=0; i<2; i++)
        codeguru[i] = array[i]
    in this mode i assign array sae of codeguru..

    int his array..
    int array[] = {112, 113};

    i would that codeguru[] contain the single number of array[0]

    so the output of variables..

    codeguru[0] //contain 1
    codeguru[1] //contain 1
    codeguru[2] // contain 2

    how can i do?

  6. #6
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645

    Re: int array..

    You seem to mixing your types.
    Is this a string or an int?

    112 decimal = 0x70
    This takes only 1 byte of the 4 bytes for an int

    array[0] = 0x70
    array[1] = 0
    array[2] = 0
    array[3] = 0

    if this is a string, then "1" is actually 0x31 - not 0x01

  7. #7
    Join Date
    Aug 2001
    Location
    Texas
    Posts
    645

    Re: int array..

    If your talking about a string reprsentation of an integer value, then you
    would have this for a "112"
    Code:
    char myString[size];
    myString[0] = 0x31  // "1"
    myString[1] = 0x31  // "1"
    myString[2] = 0x32  // "2"
    myString[3] = 0x00  // null terminator
    String data is actually stored with the least significate byte in the lowest
    byte in storage. The start of the string is on the left side.

    However, since you are working in C++, I would use std:string. It makes
    working with strings very easy. It does all the string managment.
    Last edited by cvogt61457; October 18th, 2007 at 01:45 PM.

  8. #8
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: int array..

    Is this what you are looking for ?

    Code:
    #include <string>
    #include <sstream>
    #include <iostream>
    
    using namespace std;
    
    template <typename T1, typename T2>
    T2 Convert(const T1 & t1)
    {
      std::stringstream ss;
      ss << t1;
      T2 t2 = T2();
      ss >> t2;
      return t2;
    }
    
    int main(int argc, char *argv[])
    { 
        
    
       int array[] = {112, 113};    
       
       string s = Convert<int,string>(array[0]);
    
       int * arr2 = new int[s.size()];
    
       for (int i=0; i<s.size(); ++i)
       {
           arr2[i] = Convert<char,int>(s[i]);
    
           cout << arr2[i] << "\n";
       }
    
       delete [] arr2;
       
       return 0;
    
    }

  9. #9
    Join Date
    Sep 2007
    Posts
    85

    Re: int array..

    ohh yeahhh!!


    thanks a lot dear Philip Nicoletti...
    i study it to understand the structure...

    thanks a lot!!..

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