469,097 Members | 1,274 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,097 developers. It's quick & easy.

how to convert a int[] to string[]?

How do I easily convert a int[] to a string[]?

Kind Regards,
Allan Ebdrup
Nov 17 '05 #1
6 12434
Allan Ebdrup ha scritto:
How do I easily convert a int[] to a string[]?

Kind Regards,
Allan Ebdrup


You can do this:

Example:

int[] tmp1=new int[10];
string[] tmp2=new string[tmp1.Lenght];
for(int i=0;i<tmp.Lenght;i++)
tmp2[i]=tmp1[i].ToString();

Kind Regards,
Antonio Palermo
Nov 17 '05 #2
Hi,

What about a loop?

string[] sa = new string[ intarray.Length];
int i=0;

foreach( int elem in intarray)
sa[ i++] = elem.ToString();
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Allan Ebdrup" <co****@ofir.com> wrote in message
news:ea**************@TK2MSFTNGP15.phx.gbl...
How do I easily convert a int[] to a string[]?

Kind Regards,
Allan Ebdrup

Nov 17 '05 #3
I am not sure I understand the reason or benefit to creating a string array
that contains the exact same information as the int array, but if you were
using the int array to hold the ascii values or byte values of characters
that you wanted to create in the string array then you could do something
like this.

string[] sa = new string[ intarray.Length];

for (int x = 0; x < intarray.GetUpperBound(0); x++)
sa[x] = (char)intarray[x]

Or an easier way (if you where using a byte array):
string sa = Encoding.ASCII.GetString(buff, 0, buff.GetUpperBound(0));

I have done something similiar in the past for streaming data from telnet
connections and converting the bytes to characters.

The reverse of this (to a byte array) would be:

bytearray = Encoding.ASCII.GetBytes(sa);

- or -

for (int x = 0; x<=sa.GetUpperBound(0); x++)
{
bytearray[x] = Convert.ToByte(sa[x]);
}
Hope this helps.

Gregory McCallum [MCSD]
"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

What about a loop?

string[] sa = new string[ intarray.Length];
int i=0;

foreach( int elem in intarray)
sa[ i++] = elem.ToString();
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Allan Ebdrup" <co****@ofir.com> wrote in message
news:ea**************@TK2MSFTNGP15.phx.gbl...
How do I easily convert a int[] to a string[]?

Kind Regards,
Allan Ebdrup


Nov 17 '05 #4
"gmccallum" <gm*******@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
I am not sure I understand the reason or benefit to creating a string array
that contains the exact same information as the int array


I Just wanted to use the string.Join method to join the array into a csv of
the elements, something like:
string cvs = string.Join(",", (string[])intArray));
but for some reason you cant cast from int[] to string[] and string.Join
requires a string[]

Now I do this instead:
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach(int elem in aJobFolderIds) s.Append(elem.ToString() + ",");
if(s.Length>0) s.Remove(s.Length-1, 1); //remove last comma
string csv = s.ToString();

Kind Regards,
Allan Ebdrup
Nov 17 '05 #5
Hi ,

Well if would have said so from the beggining :)

No, you cannot cast a int[] to a string[] , your solution is the best you
can get IMO.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Allan Ebdrup" <co****@ofir.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
"gmccallum" <gm*******@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
I am not sure I understand the reason or benefit to creating a string
array
that contains the exact same information as the int array


I Just wanted to use the string.Join method to join the array into a csv
of the elements, something like:
string cvs = string.Join(",", (string[])intArray));
but for some reason you cant cast from int[] to string[] and string.Join
requires a string[]

Now I do this instead:
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach(int elem in aJobFolderIds) s.Append(elem.ToString() + ",");
if(s.Length>0) s.Remove(s.Length-1, 1); //remove last comma
string csv = s.ToString();

Kind Regards,
Allan Ebdrup

Nov 17 '05 #6
Hi,

Well , he said to convert to int[] to string[]

if int[0] = 123456 then string[0] = "123456";

your code don't do that.

in fact, your code do two different things, one is creating a string[] and
the other a string.

at the end he wanted another thing ( closer to your second code )
the thing is that he did not especified that the int[] can be treated as
char , it could be 123456 .
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"gmccallum" <gm*******@discussions.microsoft.com> wrote in message
news:19**********************************@microsof t.com...
I am not sure I understand the reason or benefit to creating a string array
that contains the exact same information as the int array, but if you were
using the int array to hold the ascii values or byte values of characters
that you wanted to create in the string array then you could do something
like this.

string[] sa = new string[ intarray.Length];

for (int x = 0; x < intarray.GetUpperBound(0); x++)
sa[x] = (char)intarray[x]

Or an easier way (if you where using a byte array):
string sa = Encoding.ASCII.GetString(buff, 0, buff.GetUpperBound(0));

I have done something similiar in the past for streaming data from telnet
connections and converting the bytes to characters.

The reverse of this (to a byte array) would be:

bytearray = Encoding.ASCII.GetBytes(sa);

- or -

for (int x = 0; x<=sa.GetUpperBound(0); x++)
{
bytearray[x] = Convert.ToByte(sa[x]);
}
Hope this helps.

Gregory McCallum [MCSD]
"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

What about a loop?

string[] sa = new string[ intarray.Length];
int i=0;

foreach( int elem in intarray)
sa[ i++] = elem.ToString();
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Allan Ebdrup" <co****@ofir.com> wrote in message
news:ea**************@TK2MSFTNGP15.phx.gbl...
> How do I easily convert a int[] to a string[]?
>
> Kind Regards,
> Allan Ebdrup
>


Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Convert TextBox.Text to Int32 Problem | last post: by
6 posts views Thread by compboy | last post: by
4 posts views Thread by tshad | last post: by
9 posts views Thread by Marco Nef | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by kglaser89 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.