|
-
October 18th, 2007, 06:22 AM
#1
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!!
-
October 18th, 2007, 11:30 AM
#2
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
October 18th, 2007, 12:39 PM
#3
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!
-
October 18th, 2007, 01:00 PM
#4
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]
-
October 18th, 2007, 01:19 PM
#5
Re: int array..
 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?
-
October 18th, 2007, 01:30 PM
#6
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
-
October 18th, 2007, 01:41 PM
#7
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.
-
October 18th, 2007, 01:59 PM
#8
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;
}
-
October 18th, 2007, 02:12 PM
#9
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|