473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

string array or int array?

All this time I've been using a string array for my program, but I think
I might want to actually use the values (0s and 1s) as integers, so is
there an easy way to convert this? Should it happen when the file (of 0s
and 1s) is being read into the array, or should I read it as a string
array first, and then convert it to int? I tried one thing, but it
didn't work:

StreamReader readSwitchValues = new StreamReader(path);

string[] completeFile = readSwitchValues.ReadToEnd().Split();
int[] allSwitchValues = Int32.Parse(completeFile);
readSwitchValues.Close();
Nov 17 '05 #1
2 3978
John Salerno wrote:
All this time I've been using a string array for my program, but I think
I might want to actually use the values (0s and 1s) as integers, so is
there an easy way to convert this? Should it happen when the file (of 0s
and 1s) is being read into the array, or should I read it as a string
array first, and then convert it to int? I tried one thing, but it
didn't work:

StreamReader readSwitchValues = new StreamReader(path);

string[] completeFile = readSwitchValues.ReadToEnd().Split();
int[] allSwitchValues = Int32.Parse(completeFile);
readSwitchValues.Close();


I tried this:

StreamReader readSwitchValues = new StreamReader(path);

string[] completeFile = readSwitchValues.ReadToEnd().Split();
readSwitchValues.Close();

int[] allSwitchValues = new int[640];
int i = 0;

foreach (string status in completeFile)
allSwitchValues[i++] = Int32.Parse(status);

but the Parse method is throwing an exception.

"Input string was not in a correct format."
Nov 17 '05 #2
I see in your snippet, you parse an array of string to get an array of
int. That is not possible. If you want that, try something like
following:

int[] ParseIntValues(string[] stringValues)
{
int theInts = new int[stringValues.Length];
foreach(int i = 0, n = stringValues.Length, i < n; i++)
{
theInts[i] = Int32.Parse(stringValues[i], NumberStyle.Integer,
CultureInfo.InvariantCulture);
}

return theInts;
}

I see you made some similar posts about this. If you describe what you
are trying to do instead, we could discuss the solution.

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
22
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
8
by: Jeff Johnson | last post by:
Hi, I've begun converting an ASP site over to .NET and I'm a novice at both the new platform as well as C#. I have a COM+ object that returns a string array when it is called. The size of...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.