来自int []的strcpy? [英] strcpy from int[]?

查看:188
本文介绍了来自int []的strcpy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个int'和一个字符串数组。我试图将int数组

复制到字符串中,但我没有成功。我能想到的唯一解决方案

是strcpy还是memcpy。


我编写了以下测试程序但是在下面的例子中str是空的。


我想在stdlib.h中使用itoa()但itoa()必须是过时的,gcc

找不到这个功能。


有什么建议吗?


操作系统:linux

编译器:gcc v:4.1.1

#include< iostream>

#include< string>


使用命名空间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;

返回0;

}

问候

解决方案

2006年7月26日星期三04:39:38 + 0100,Ural Mutlu写道:





我有一个int'和一个字符串数组。我试图将int数组

复制到字符串中,但我没有成功。我能想到的唯一解决方案

是strcpy还是memcpy。


我编写了以下测试程序但是在下面的例子中str是空的。


我想在stdlib.h中使用itoa()但itoa()必须是过时的,gcc

找不到这个功能。


有什么建议吗?


操作系统:linux

编译器:gcc v:4.1.1


#include< iostream>

#include< string>


使用命名空间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;

}


问候



我应该补充说我不打算转换int'' s to char''s。


我的意思是如果int的大小是4测试,我必须将其表示为4个字符。

基本上,一些数据存储在字符串而不是int中。


那个''为什么我认为memcpy是解决方案,但不知何故上面的

例子并没有给我一个结果。


问候


Ural Mutlu写道:





我有一个int数组'和一根绳子。我试图将int数组

复制到字符串中,但我没有成功。我能想到的唯一解决方案

是strcpy还是memcpy。



为什么?


我编写了以下测试程序但是在下面的例子中str是空的。


我想在stdlib.h中使用itoa()但itoa()必须是过时的,gcc

找不到这个功能。


有什么建议吗?



什么你真的试图实现吗?你想要字符串中的整数或它们的字符等价的按位

表示吗?


#include< iostream>

#include< string>


使用命名空间std;



为什么?
< blockquote class =post_quotes>
int main(){


string str("");

int i [2] = {0,0};


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



你不能使用c_str()返回的值作为左值,因为你的
编译器会告诉你是否删除Vile cast。


-

Ian Collins。


2006年7月26日星期三16:41:22 +1200,Ian Collins写道:


Ural Mutlu写道:


>

我有一个int'和一个字符串数组。我试图将int数组复制到字符串中,但我还没有成功。我能想到的唯一解决方案是strcpy或memcpy。



为什么?


>我写了下面的测试程序但是在下面的例子中str是空的。

我想在stdlib.h中使用itoa()但itoa()必须是过时的,gcc
没有找到这个功能。

有什么建议吗?



你想要实现什么?你想要你的字符串中的整数或它们的字符等价的按位

表示吗?



bitwise


>


> #include< iostream>
#include< string>

使用命名空间std;



为什么?


> int main(){

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

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



你不能使用c_str()返回的值作为左值,因为你的
编译器会告诉你是否删除卑鄙的演员。



没有编译器不会抱怨并且它有效,确实我在某个地方看到这个

else这就是我使用的原因它。 c_str()返回一个const char *,因此

需要将它转换为char *。我知道这很难看但很有效。


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

解决方案

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


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.


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.


这篇关于来自int []的strcpy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆