Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello frnds , i need help

i have string of 1's and 0's.
Now i want to take that string value in an array of type int.
please help
e.g
s="10101111100011111";
Posted

As far as I know there is no direct method of achieving that. Try the code below:

C#
public static int[] StringToInts(string myString)
{
List<int> ints = new List<int>();
string[] strings = myString.Split(',');

foreach (string s in strings)
{
int i;
if (int.TryParse(s.Trim(), out i))
{
ints.Add(i);
}
}
return ints.ToArray();
}
 
Share this answer
 
Comments
Noel3090 4-Apr-11 9:55am    
thanks guys!!! :)
here is 1 more method
String s = "1234";
int[] intArray = new int[s.length()];

for (int i = 0; i < s.length(); i++) {
intArray[i] = Character.digit(s.charAt(i), 10);
}
Unless you have different requirements, 1 single integer can hold such a binary (I guess) string value:

Java
int k =  Integer.parseInt("10101111100011111",2);


:-)
 
Share this answer
 
Comments
Wild-Programmer 4-Apr-11 8:54am    
Bravo, master stroke, my 5+ :)
CPallini 4-Apr-11 8:56am    
Thanks :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900