473,473 Members | 1,768 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 12739
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
2
by: paul gao via .NET 247 | last post by:
hi all. In my program I need to convert a double number to stringrepresentation in fraction format and vise versa. For example,convert 0.75 to string "3/4" and convert string "1/2" to 0.5.The class...
2
by: Learner | last post by:
Hello, I am trying to store the data entered in a webform in the database. I have few Int and one SmallDateTime filed in my table in SQL Server 2005 database. I have made a storedproc to store the...
6
by: compboy | last post by:
Can anyone help me about this. I have been trying all the ways I knew and I could find but just didnt work. I have tried: using itoa but it says that it doesnt have that function. and...
6
by: Ken Fine | last post by:
This is a basic question. What is the difference between casting and using the Convert.ToXXX methods, from the standpoint of the compiler, in terms of performance, and in other ways? e.g. ...
4
by: tshad | last post by:
I am trying to convert a string character to an int where the string is all numbers. I tried: int test; string stemp = "5"; test = Convert.ToInt32(stemp); But test is equal to 53.
9
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() ==...
0
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.