将int列表作为字符串 [英] put list of int as string
问题描述
假设我有一个int数组,如何将它们组合起来构建一个
字符串?
Eg
int data [0] == 77
int data [1] == 121
int data [2] == 32 >
int data [3] == 84
int data [4] == 101
int data [5] == 115
int data [6] == 116
字符串将是我的测试
我不确定以下算法是否实现我的目的......
char * mystring;
for(i = 0; i< 7; i ++)
strcat(mystring,char (数据(i));
Magix写道:
让我说我有一个int数组,如何将它们组合在一起构造一个
字符串?
int data [0] == 77
int data [1] == 121 > int data [2] == 32
int data [3] == 84
int data [4] == 101
int data [5] == 115
int数据[6] == 116
字符串将是我的测试
我不确定以下算法是否达到我的目的...
char * mystring;
for(i = 0; I< 7; i ++)
strcat(mystring,char(data(i));
一个字符串只是一个charS数组,所以你要分配元素n
的int数组到char数组的元素n。
char * intarr2str(int * arr,int len)
{
char * str;
int i;
str = malloc(len + 1); / * room for null * /
for(i = 0; i< len; ++ i)
str [i] = arr [i];
str [i] = NULL; / * null终止* /
返回str; / *调用者必须免费* /
}
在你的情况下你会用这样的东西:
char * mystring;
mystring = intarr2str(数据,7);
出于好奇,你为什么要这样做?
John
2004年6月22日星期二23:09:36 -0400,John Ilves
< jo ******* @ adelphia .net>在comp.lang.c中写道:
Magix写道:
让我说我有一个int数组,如何将它们组合在一起构建一个
字符串?
例如
int数据[0] == 77
int data [1] == 121
int data [2] == 32
int data [3] == 84
int data [4 ] == 101
int data [5] == 115
int data [6] == 116
字符串将是我的测试
我不确定以下算法是否达到我的目的...
char * mystring;
for(i = 0; I< 7; i ++)
strcat(mystring,char(data(i));
一个字符串只是一个charS数组,所以你要分配元素n
对于char数组的元素n的int数组。
char * intarr2str(int * arr,int len)
{
char * str;
int i;
str = malloc(len + 1); / * null for null * /
for(i = 0; i< len; ++ i)
str [i] = arr [i];
str [i] = NULL; / * null终止* /
返回str; / *调用者必须免费* /
}
[snip]
添加:
#include< stdlib.h>
....对于malloc'的原型和空指针的定义
常量NULL,这是两个不同编译器的结果:
========
编译......
样本.c
C:\Program Files\Microsoft Visual
Studio\MyProjects\sample\sample.c(13):警告C4047:''= :''char''
的间接水平与''void *'不同
sample.obj - 0个错误,1个警告(s)
========
....和:
= =======
Wedit输出窗口构建:Tue Jun 22 23:46:32 2004
错误c:\prog\lcc\projects \\ \ nsample \sample.c:13个操作数= =
非法类型''char''和''指向无效''
编译+链接时间:0.1秒,返回代码:1
========
谁告诉你NULL等同于''\ 0''?当然不是C
语言标准,而不是我用过的大多数编译器。
-
Jack Klein
主页: http://JK-Technology.Com
常见问题解答
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c ++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
感谢。它是一些串行通信的东西。
如果是16位数据(2字节)怎么办,怎样才能使用缓冲区保存数据?
结束,我仍然希望将结果作为一个字符串。
" John Ilves" <乔******* @ adelphia.net>在消息中写道
news:40 ************** @ adelphia.net ...Magix写道:
让我说我有一个int数组,怎么把它们组合起来构造一个
字符串?
例如
int data [0] == 77
int data [1] == 121
int data [2] == 32
int data [3] == 84
int data [4] == 101
int data [5] == 115
int data [6] == 116
字符串将是我的测试
我''我不确定下面的算法是否达到我的目的...
char * mystring;
for(i = 0; i< 7; i ++)
strcat(mystring,char(data(i) );
一个字符串只是一个charS数组,所以你想要将int数组的元素n分配给char数组的元素n。
char * intarr2str(int * arr,int len)
{
char * str;
int i;
str = malloc(len + 1); / *空的空间* /
for(i = 0; i< len; ++ i)
str [i] = arr [i];
str [i] = NULL; / * null终止* /
返回str; / *来电者必须免费* /
}
在你的情况下你会用这样的东西:
char * mystring;
mystring = intarr2str(数据,7);
出于好奇,你为什么要这样做?
John
Hi,
let say I have an int array, how can I put them together to construct a
string?
E.g
int data[0] == 77
int data[1] == 121
int data[2] == 32
int data[3] == 84
int data[4] == 101
int data[5] == 115
int data[6] == 116
the string will be "My Test"
I''m not sure if below algorithm achieve my purpose...
char *mystring;
for (i=0; i<7; i++)
strcat(mystring, char(data(i));
Magix wrote:Hi,
let say I have an int array, how can I put them together to construct a
string?
E.g
int data[0] == 77
int data[1] == 121
int data[2] == 32
int data[3] == 84
int data[4] == 101
int data[5] == 115
int data[6] == 116
the string will be "My Test"
I''m not sure if below algorithm achieve my purpose...
char *mystring;
for (i=0; i<7; i++)
strcat(mystring, char(data(i));
Well a string is just an array of charS, so you want to assign element n
of the int array to element n of the char array.
char* intarr2str(int* arr, int len)
{
char* str;
int i;
str = malloc(len + 1); /* room for null */
for(i = 0; i < len; ++i)
str[i] = arr[i];
str[i] = NULL; /* null terminate */
return str; /* caller must free */
}
In your case you would use that with something like this:
char* mystring;
mystring = intarr2str(data, 7);
Out of curiosity, why do you want to do this?
John
On Tue, 22 Jun 2004 23:09:36 -0400, John Ilves
<jo*******@adelphia.net> wrote in comp.lang.c:
Magix wrote:Hi,
let say I have an int array, how can I put them together to construct a
string?
E.g
int data[0] == 77
int data[1] == 121
int data[2] == 32
int data[3] == 84
int data[4] == 101
int data[5] == 115
int data[6] == 116
the string will be "My Test"
I''m not sure if below algorithm achieve my purpose...
char *mystring;
for (i=0; i<7; i++)
strcat(mystring, char(data(i));
Well a string is just an array of charS, so you want to assign element n
of the int array to element n of the char array.
char* intarr2str(int* arr, int len)
{
char* str;
int i;
str = malloc(len + 1); /* room for null */
for(i = 0; i < len; ++i)
str[i] = arr[i];
str[i] = NULL; /* null terminate */
return str; /* caller must free */
}
[snip]
Adding:
#include <stdlib.h>
....for malloc''s prototype and a definition of the null pointer
constant NULL, here is the result from two different compilers:
========
Compiling...
sample.c
C:\Program Files\Microsoft Visual
Studio\MyProjects\sample\sample.c(13) : warning C4047: ''='' : ''char ''
differs in levels of indirection from ''void *''
sample.obj - 0 error(s), 1 warning(s)
========
....and:
========
Wedit output window build: Tue Jun 22 23:46:32 2004
Error c:\prog\lcc\projects\sample\sample.c: 13 operands of = have
illegal types ''char'' and ''pointer to void''
Compilation + link time:0.1 sec, Return code: 1
========
Whoever told you that NULL is equivalent to ''\0''? Certainly not the C
language standard, and not most of the compilers I have ever used.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Thanks. it for some serial communication stuff.
What if it is 16 bits data (2 bytes), how can I use buffer hold the data ?
In the end, I still want the outcome as a string.
"John Ilves" <jo*******@adelphia.net> wrote in message
news:40**************@adelphia.net...Magix wrote:Hi,
let say I have an int array, how can I put them together to construct a
string?
E.g
int data[0] == 77
int data[1] == 121
int data[2] == 32
int data[3] == 84
int data[4] == 101
int data[5] == 115
int data[6] == 116
the string will be "My Test"
I''m not sure if below algorithm achieve my purpose...
char *mystring;
for (i=0; i<7; i++)
strcat(mystring, char(data(i));
Well a string is just an array of charS, so you want to assign element n
of the int array to element n of the char array.
char* intarr2str(int* arr, int len)
{
char* str;
int i;
str = malloc(len + 1); /* room for null */
for(i = 0; i < len; ++i)
str[i] = arr[i];
str[i] = NULL; /* null terminate */
return str; /* caller must free */
}
In your case you would use that with something like this:
char* mystring;
mystring = intarr2str(data, 7);
Out of curiosity, why do you want to do this?
John
这篇关于将int列表作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!