473,498 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strcpy from int[]?

Hi,

I have an array of int's and a string. I am trying to copy the int array
into the string but I haven't been successful. The only solution I can think
of is strcpy or memcpy.

I wrote the following test program but in the example below str is empty.

I thought of using itoa() in the stdlib.h but itoa() must be obsolete, gcc
doesn't find the function.

Any suggestions?

OS: linux
compiler: gcc v:4.1.1
#include <iostream>
#include <string>

using namespace std;

int main() {

string str("");
int i[2]={0,0};

strncpy((char*)str.c_str(),(char*)i, sizeof(i));
//memcpy((void*)str.c_str(),(void*)i, sizeof(i));
cout<<str<<endl;
return 0;
}
Regards
Jul 26 '06 #1
10 8906
On Wed, 26 Jul 2006 04:39:38 +0100, Ural Mutlu wrote:
Hi,

I have an array of int's and a string. I am trying to copy the int array
into the string but I haven't been successful. The only solution I can think
of is strcpy or memcpy.

I wrote the following test program but in the example below str is empty.

I thought of using itoa() in the stdlib.h but itoa() must be obsolete, gcc
doesn't find the function.

Any suggestions?

OS: linux
compiler: gcc v:4.1.1
#include <iostream>
#include <string>

using namespace std;

int main() {

string str("");
int i[2]={0,0};

strncpy((char*)str.c_str(),(char*)i, sizeof(i));
//memcpy((void*)str.c_str(),(void*)i, sizeof(i));
cout<<str<<endl;
return 0;
}
Regards
I should probably add that I am not looking to convert the int's to char's.

I mean if the size of int is 4 bytes, I have to represent it as 4 char's.
Basically, some data is stored in a string rather than int.

That's why I thought memcpy is the solution, but somehow the above
example doesn't give me a result.

regards
Jul 26 '06 #2
Ural Mutlu wrote:
Hi,

I have an array of int's and a string. I am trying to copy the int array
into the string but I haven't been successful. The only solution I can think
of is strcpy or memcpy.
Why?
I wrote the following test program but in the example below str is empty.

I thought of using itoa() in the stdlib.h but itoa() must be obsolete, gcc
doesn't find the function.

Any suggestions?
What are you actualy trying to achieve? Do you want the bitwise
representation of the ints in your string or their character equivalent?
#include <iostream>
#include <string>

using namespace std;
Why?
int main() {

string str("");
int i[2]={0,0};

strncpy((char*)str.c_str(),(char*)i, sizeof(i));
You can't use the value returned by c_str() as an lvalue, as your
compiler will tell you if you remove the vile cast.

--
Ian Collins.
Jul 26 '06 #3
On Wed, 26 Jul 2006 16:41:22 +1200, Ian Collins wrote:
Ural Mutlu wrote:
>Hi,

I have an array of int's and a string. I am trying to copy the int array
into the string but I haven't been successful. The only solution I can think
of is strcpy or memcpy.
Why?
>I wrote the following test program but in the example below str is empty.

I thought of using itoa() in the stdlib.h but itoa() must be obsolete, gcc
doesn't find the function.

Any suggestions?
What are you actualy trying to achieve? Do you want the bitwise
representation of the ints in your string or their character equivalent?
bitwise
>
>#include <iostream>
#include <string>

using namespace std;
Why?
>int main() {

string str("");
int i[2]={0,0};

strncpy((char*)str.c_str(),(char*)i, sizeof(i));

You can't use the value returned by c_str() as an lvalue, as your
compiler will tell you if you remove the vile cast.

no the compiler doesn't complain and it work, indeed I saw this somewhere
else and that's why I use it. c_str() returns a const char*, therefore the
need to cast it to char*. I know it's ugly but works.
Jul 26 '06 #4
>> string str("");
int i[2]={0,0};

strncpy((char*)str.c_str(),(char*)i, sizeof(i));

You can't use the value returned by c_str() as an lvalue, as your
compiler will tell you if you remove the vile cast.


no the compiler doesn't complain and it work, indeed I saw this somewhere
else and that's why I use it. c_str() returns a const char*, therefore the
need to cast it to char*. I know it's ugly but works.

sorry I misunderstood your message, too early in the morning..

you are right...
Jul 26 '06 #5
Ural Mutlu wrote:
On Wed, 26 Jul 2006 04:39:38 +0100, Ural Mutlu wrote:
>Hi,

I have an array of int's and a string. I am trying to copy the int array
into the string but I haven't been successful. The only solution I can
think of is strcpy or memcpy.

I wrote the following test program but in the example below str is empty.

I thought of using itoa() in the stdlib.h but itoa() must be obsolete,
gcc doesn't find the function.

Any suggestions?

OS: linux
compiler: gcc v:4.1.1
#include <iostream>
#include <string>

using namespace std;

int main() {

string str("");
int i[2]={0,0};

strncpy((char*)str.c_str(),(char*)i, sizeof(i));
//memcpy((void*)str.c_str(),(void*)i, sizeof(i));
cout<<str<<endl;
return 0;
}
Regards

I should probably add that I am not looking to convert the int's to
char's.
Yes.
I mean if the size of int is 4 bytes, I have to represent it as 4 char's.
Basically, some data is stored in a string rather than int.

That's why I thought memcpy is the solution, but somehow the above
example doesn't give me a result.
Try:

#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;

int main() {
string str("");
int i[2]={48908902,4090799};
char const * c_begin
= static_cast< char* >( static_cast< void* >( &i[0] ) );
char const * c_end = c_begin + sizeof(i);
copy( c_begin, c_end, inserter( str, str.end() ) );
cout<<str<<endl;
return 0;
}

Beware that the code is bound to invoke undefined behavior as the standard
does not impose rigid restrictions on the internal representation of
integers. In particular, you may find that endian-ness enters the picture.
Best

Kai-Uwe Bux
Jul 26 '06 #6
Ural Mutlu wrote:
On Wed, 26 Jul 2006 16:41:22 +1200, Ian Collins wrote:
>>
What are you actualy trying to achieve? Do you want the bitwise
representation of the ints in your string or their character equivalent?


bitwise
Why on Earth would you want to do that? Consider what happens with 0.
>
>>>#include <iostream>
#include <string>

using namespace std;

Why?

>>>int main() {

string str("");
int i[2]={0,0};

strncpy((char*)str.c_str(),(char*)i, sizeof(i));

You can't use the value returned by c_str() as an lvalue, as your
compiler will tell you if you remove the vile cast.

no the compiler doesn't complain and it work, indeed I saw this somewhere
else and that's why I use it. c_str() returns a const char*, therefore the
need to cast it to char*. I know it's ugly but works.
Nonsense. It may appear to work, or even work with you current
implementation, but it's wrong. Consider what happens if the
implementation of std::string doesn't allocate a buffer until it is
required.

Either way, you will end up with an invalid string object, all of its
internal state with be for zero length, but you will have bladdered its
buffer. Any data you have written in will be lost because there won't
be a way to get it back.

--
Ian Collins.
Jul 26 '06 #7
>
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;

int main() {
string str("");
int i[2]={48908902,4090799};
char const * c_begin
= static_cast< char* >( static_cast< void* >( &i[0] ) );
char const * c_end = c_begin + sizeof(i);
copy( c_begin, c_end, inserter( str, str.end() ) );
cout<<str<<endl;
return 0;
}

Beware that the code is bound to invoke undefined behavior as the standard
does not impose rigid restrictions on the internal representation of
integers. In particular, you may find that endian-ness enters the picture.
Best

Kai-Uwe Bux

thanks,

got it to work in a similar way to what you said.

I still don't understand why memcpy or strcpy don't work. The code
above copies one char at a time until the last element is copied. The
difference is that memcpy copies a chunk of memory given with the
pointer and size. Basically they both copy memory and they should do the
same job?

regards,
ural
Jul 26 '06 #8
Ural Mutlu wrote:
>>#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;

int main() {
string str("");
int i[2]={48908902,4090799};
char const * c_begin
= static_cast< char* >( static_cast< void* >( &i[0] ) );
char const * c_end = c_begin + sizeof(i);
copy( c_begin, c_end, inserter( str, str.end() ) );
cout<<str<<endl;
return 0;
}

Beware that the code is bound to invoke undefined behavior as the standard
does not impose rigid restrictions on the internal representation of
integers. In particular, you may find that endian-ness enters the picture.
Best

Kai-Uwe Bux

thanks,

got it to work in a similar way to what you said.

I still don't understand why memcpy or strcpy don't work. The code
above copies one char at a time until the last element is copied. The
difference is that memcpy copies a chunk of memory given with the
pointer and size. Basically they both copy memory and they should do the
same job?
Because you can't write to the value returned by c_str().

--
Ian Collins.
Jul 26 '06 #9
Ural Mutlu wrote:
>>
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>

using namespace std;

int main() {
string str("");
int i[2]={48908902,4090799};
char const * c_begin
= static_cast< char* >( static_cast< void* >( &i[0] ) );
char const * c_end = c_begin + sizeof(i);
copy( c_begin, c_end, inserter( str, str.end() ) );
cout<<str<<endl;
return 0;
}

Beware that the code is bound to invoke undefined behavior as the
standard does not impose rigid restrictions on the internal
representation of integers. In particular, you may find that endian-ness
enters the picture.
Best

Kai-Uwe Bux


thanks,

got it to work in a similar way to what you said.

I still don't understand why memcpy or strcpy don't work. The code
above copies one char at a time until the last element is copied. The
difference is that memcpy copies a chunk of memory given with the
pointer and size. Basically they both copy memory and they should do the
same job?
Nope. For one thing, note that the string may need to increase in length
while you are appending characters. So, reallocations might occur issued
behind your back and out of your controll by the string class. Thus, if you
try to force memcpy to write into the buffer owned by the string, you are
likely to write beyond its end. Also, how is memcopy supposed to notify the
string object that it became longer? Strings do not use the 0 char as a
terminator, instead they keep track of their length.

In short, all that you do using memcpy is to trash the internal data of the
string. The standard was written with some possible implementations in
mind. Therefore, it declares what you try undefined behavior. And you got
the worst incarnation of it.
Best

Kai-Uwe Bux
Jul 26 '06 #10

"Ural Mutlu" <ur*******@gmail.comskrev i meddelandet
news:pa****************************@gmail.com...
>>#include <iostream>
#include <string>

using namespace std;
Why?
>>int main() {

string str("");
int i[2]={0,0};

strncpy((char*)str.c_str(),(char*)i, sizeof(i));

You can't use the value returned by c_str() as an lvalue, as your
compiler will tell you if you remove the vile cast.


no the compiler doesn't complain and it work, indeed I saw this
somewhere
else and that's why I use it. c_str() returns a const char*,
therefore the
need to cast it to char*. I know it's ugly but works.
The compiler doesn't complain, because the C-style casts tell it to
shut up. It knows that it doesn't work, but the code tells it do do it
anyway!

If you really, really, *really* want to copy the bytes of i to a
string, it is much easier to use the string's constructor. Just tell
the compiler that you magically know that i is really a pointer to
char ("Honest, gov! I wouldn't lie."):

std::string str(reinterpret_cast<const char*>(i), sizeof i);

That's it! But not very useful.
Bo Persson
Jul 26 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3316
by: Paul Sheer | last post by:
I need to automatically search and replace all fixed size buffer strcpy's with strncpy's (or better yet, strlcpy's) as a security and stability audit. The code base is large and it is not feasable...
8
3289
by: RonHiler | last post by:
My copy constructor is crashing my program, and I can't figure out why. I'll try to make the code listing as short as I can. Here are the two headers: class BreakthroughClass { public:...
11
3124
by: leonkatz | last post by:
What operators do I have to overload to be able to do a strcpy(pStr, myObj) ? Example: class MyClass { public: MyClass(char* pStr){ if(pStr == NULL) { str = NULL;
9
7737
by: Ape Ricket | last post by:
Hi. During my program's set-up phase where it reads in the arguments it was invoked with, I programmed this: if (strcmp(argv,"-G") ==0) { geom_scaling = ON; if (i < argc-1)...
4
2052
by: a2x | last post by:
Hi, I've fixed this error, but I don't know why it occurs. Do you? Code: #include <stdlib.h> #include <string.h> void another(); int main()
302
18272
by: Lee | last post by:
Hi Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). Is this due to the possibility of array overflow? Is it correct that the program...
3
16766
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'...
55
7028
by: Jake Thompson | last post by:
I need to copy a value into a char * field. I am currently doing this strcpy(cm8link.type,"13"); but I get an error of error C2664: 'strcpy' : cannot convert parameter 1 from 'const char'...
9
2627
by: jim | last post by:
i want to make a c file that i can 'scanf ' students scores of 2 classes and their names , and i want it to get the sum of the 2 scores and make them in order .at last 'printf' /*am sorry,my...
77
8261
by: arnuld | last post by:
I have created my own implementation of strcpy library function. I would like to have comments for improvements: /* My version of "strcpy - a C Library Function */ #include <stdio.h>...
0
7002
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7165
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4910
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4590
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1419
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
291
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.