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