473,756 Members | 3,051 Online
Bytes | Software Development & Data Engineering Community
+ 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 12767
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.Lengh t;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.co m> wrote in message
news:ea******** ******@TK2MSFTN GP15.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.GetUpp erBound(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.GetUpperBo und(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.GetUpperB ound(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.co m> wrote in message
news:ea******** ******@TK2MSFTN GP15.phx.gbl...
How do I easily convert a int[] to a string[]?

Kind Regards,
Allan Ebdrup


Nov 17 '05 #4
"gmccallum" <gm*******@disc ussions.microso ft.com> wrote in message
news:19******** *************** ***********@mic rosoft.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.Str ingBuilder s = new System.Text.Str ingBuilder();
foreach(int elem in aJobFolderIds) s.Append(elem.T oString() + ",");
if(s.Length>0) s.Remove(s.Leng th-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.co m> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
"gmccallum" <gm*******@disc ussions.microso ft.com> wrote in message
news:19******** *************** ***********@mic rosoft.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.Str ingBuilder s = new System.Text.Str ingBuilder();
foreach(int elem in aJobFolderIds) s.Append(elem.T oString() + ",");
if(s.Length>0) s.Remove(s.Leng th-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*******@disc ussions.microso ft.com> wrote in message
news:19******** *************** ***********@mic rosoft.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.GetUpp erBound(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.GetUpperBo und(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.GetUpperB ound(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.co m> wrote in message
news:ea******** ******@TK2MSFTN GP15.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
9767
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 numeric value according to an arbitrary regular expression.
3
10289
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 and there you have it. So I enquired and found the Convert class with it's promising ToInt32 method, great... but it doesn't work. The thing keeps throwing Format Exceptions all over the place. What is the "C#" way to do this??? code int wmin,...
2
11527
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 will only need to handle numbers that increment by1/32. That is 1/32, 2/32, 3/32.... 31/32, 1. If the doublenumber is 0.28 which is between 1/4 and 9/32, the string shouldbe 9/32(go with the bigger number). TIA --------------------------------...
2
2241
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 values. My parameters in the database are like this @DealerShipID int, @ReturnDate smallDateTime,
6
34155
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 also
6
4265
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. this.ContentID = (int)ci.Conid; vs. this.ContentID = Convert.ToInt32(ci.Conid); I tend to use the latter form because it seems more descriptive to me, but it would be good to know what's best practice. I'm guessing those methods
4
5078
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
17567
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() == "3"; Convert<HelloWorld>::Get() == "HelloWorld"; The reason I need this is that in our design every class of a certain
0
10780
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 inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
9273
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10032
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9841
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8712
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7244
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6534
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5141
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.