This question already has answers here:
Split String in C# without delimiter (sort of)
(2 answers)
Closed 2 years ago.
how to separate something from 83726473827 to
int[0] = 8;
int[1] = 3;
int[2] = 7;
etc...
I don't want use any separator
You can use Linq extension method Select with char.getNumericValue() like this:
using System.Linq;
string str = "83726473827";
int[] array = str.Select(c => (int)char.GetNumericValue(c)).ToArray();
foreach ( int value in array )
Console.WriteLine(value);
We take each char of the string and convert them to numeric value as integer, then we take the result as an array.
This code assumes that the string contains only digits else we need to take in consideration other cases.
Output
8
3
7
2
6
4
7
3
8
2
7
Related
This question already has answers here:
char + char = int? Why?
(15 answers)
Closed 4 years ago.
This is something they included as part of my course, just wondering why it does this and what they were trying to show with it but can't seem to figure it out. Is it some sort of principle when trying to concatenate chars to numbers?
Am I right in assuming that 'z' is a char because it's in single quotes here?
Is it some sort of error because you shouldn't write stuff like this?
Thanks in advance!
z is char value, char is basically a number. z will be implicitly converted to int (z code is 122), that's why 3 + 'z' + 4 == 129. It will be converted to int because in statement 3 + 'z' 3 is int, so result of addition will be also int.
In C# char is a 16 bit numeric value which represents a unicode character. So in your case z is implicitly evaluated as 122. So 3 + 122 + 4 equals 129.
This question already has answers here:
Convert int to a bit array in .NET
(10 answers)
Closed 7 years ago.
I want to convert my base 10 number to base 2 and then store parts in an array.
Here are two examples:
my value is 5 so it will be converted to 101 then I have an array like this: {1,0,1}
or my value is 26 so it will be converted to 11010 then I will have an array like this: {0,1,0,1,1}
Thank you in advance for your time and consideration.
To convert the int 'x'
int x = 3;
One way, by manipulation on the int :
string s = Convert.ToString(x, 2); //Convert to binary in a string
int[] bits= s.PadLeft(8, '0') // Add 0's from left
.Select(c => int.Parse(c.ToString())) // convert each char to int
.ToArray(); // Convert IEnumerable from select to Array
Alternatively, by using the BitArray class-
BitArray b = new BitArray(new byte[] { x });
int[] bits = b.Cast<bool>().Select(bit => bit ? 1 : 0).ToArray();
Source: https://stackoverflow.com/a/6758288/1560697
This question already has answers here:
Convert integer to hexadecimal and back again
(11 answers)
Closed 8 years ago.
This is a small snippet of the code I have written in Visual Studio.
I need to convert the int add into an hex value (later into a string) as I am accessing the memory of a uC. But when I view the variable during debugging it displays it as a string "add16". What could I be doing wrong?
for (int add = 0; add <= 0xfffff; )
{
for (int X = 0; X <= 15; X++)
{
string address = add.ToString("add16");
addr = Convert.ToString(address);
port.WriteLine(String.Format("WRBK:{0}:{1}", addr, parts[X]));
add++;
}
}
There is a simple and very convenient method that takes an integer and returns a representation of it as a string in hexadecimal notation
string address = Convert.ToString(add, 16);
So, perhaps, your internal loop could be rewritten as
port.WriteLine(String.Format("WRBK:{0}:{1}", Convert.ToString(add++, 16), parts[X]));
and using the standard numeric format strings specifiers reduced to
port.WriteLine(String.Format("WRBK:{0:X}:{1}", add++, parts[X]));
You can tell .NET's String.Format to use hex output for the integer, eliminating any code to hex conversion, use:
port.WriteLine(String.Format("WRBK:{0:X}:{1}", add, parts[X]));
This question already has answers here:
Why does integer division in C# return an integer and not a float?
(8 answers)
Closed 8 years ago.
I have 2 input int values that need do be divided and sen back as a string with 6 decimals.
int a = 240
int b = 1440.
I want to divide them and send back a text string with 0,166667
I have tried many code examples but none have worked.
You need to convert at least one to a decimal value:
double result = (double)a / b;
or
decimal result = (decimal)a / b;
On "decimal vs. double" see THIS question.
This question already has answers here:
A clear, layman's explanation of the difference between | and || in c#?
(11 answers)
Closed 8 years ago.
I need to take two bytes and OR them together, a rather simple task. However, I do not know the proper syntax to OR the two bytes together.
byte First = 0x03;
byte Second = 0x15;
//Need to or Them
byte Result = First || Second; //This syntax does not work in C#
You need | Operator
byte Result = (byte)(First | Second);
|| is a logical or not bitwise so you need:
byte Result = (byte) (First | Second);