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

  • Thread starter Thread starter Allan Ebdrup
  • Start date Start date
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=tmp1.ToString();



Kind Regards,
Antonio Palermo
 
Hi,

What about a loop?

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

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


cheers,
 
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 ) said:
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 said:
How do I easily convert a int[] to a string[]?

Kind Regards,
Allan Ebdrup
 
gmccallum said:
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
 
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 said:
gmccallum said:
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
 
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 said:
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 ) said:
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 said:
How do I easily convert a int[] to a string[]?

Kind Regards,
Allan Ebdrup
 
Back
Top