C# 如何将int数组转换为int?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9564800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert int array to int?
提问by user1172635
What I would like to learn how to do is to convert an int array to an int in C#.
我想学习如何做的是在 C# 中将 int 数组转换为 int。
However I want to append the int with the values from the array.
但是我想用数组中的值附加 int 。
Example:
例子:
int[] array = {5, 6, 2, 4};
Would be converted into an int that equals 5624.
将转换为等于 5624 的 int。
Thanks for any help in advance.
提前感谢您的任何帮助。
回答by Dor Cohen
simply multiply each number with 10^ his place in the array.
只需将每个数字乘以 10^ 他在数组中的位置。
int[] array = { 5, 6, 2, 4 };
int finalScore = 0;
for (int i = 0; i < array.Length; i++)
{
finalScore += array[i] * Convert.ToInt32(Math.Pow(10, array.Length-i-1));
}
回答by Daniel Pe?alba
Try the following:
请尝试以下操作:
int[] intArray = new int[] { 5, 4, 6, 1, 6, 8 };
int total = 0;
for (int i = 0; i < intArray.Length; i++)
{
int index = intArray.Length - i - 1;
total += ((int)Math.Pow(10, index)) * intArray[i];
}
回答by Benni
This would be easy, if you have understood how the decimal system works.
如果您了解十进制系统的工作原理,这将很容易。
So let me explain that for you: A decimal digit contains single digits by base ten.
因此,让我为您解释一下:十进制数字包含以十为底的单个数字。
This means you have to iterate through this array (backwards!) and multiply by 10^
这意味着你必须遍历这个数组(向后!)并乘以 10^
For an example 5624 means: (5*10^3) + (6*10^2) + (2*10^1) + (4*10^0)
例如 5624 表示:(5*10^3) + (6*10^2) + (2*10^1) + (4*10^0)
Please consider also: http://en.wikipedia.org/wiki/Horner_scheme
回答by Mulesoft Developer
Use this code you just want to concatenate you int array so use the following code
使用此代码您只想连接您的 int 数组,因此请使用以下代码
String a;
int output;
int[] array = {5, 6, 2, 4};
foreach(int test in array)
{
a+=test.toString();
}
output=int.parse(a);
//where output gives you desire out put
This is not an exact code.
这不是一个确切的代码。
回答by Shadow Wizard is Ear For You
Another simple way:
另一种简单的方法:
int[] array = {5, 6, 2, 4};
int num;
if (Int32.TryParse(string.Join("", array), out num))
{
//success - handle the number
}
else
{
//failed - too many digits in the array
}
Trick here is making the array a string of digits then parsing it as integer.
这里的技巧是使数组成为一串数字,然后将其解析为整数。
回答by Rich
This will do it:
这将做到:
public int DoConvert(int[] arr)
{
int result = 0;
for (int i=0;i<arr.Length;i++)
result += arr[i] * Math.Pow(10, (arr.Length-1)-i);
return result;
}
回答by juergen d
int result = 0;
int[] arr = { 1, 2, 3, 4};
int multipicator = 1;
for (int i = arr.Length - 1; i >= 0; i--)
{
result += arr[i] * multipicator;
multipicator *= 10;
}
回答by Adrian Thompson Phillips
And just for fun...
而且只是为了好玩……
arr.Select((item, index) => new { Item = item, Power = arr.Length - (index - 1) }).ToList().ForEach(item => total += (int)(Math.Pow(10, item.Power) * item.Item));
回答by RichK
int output = array
.Select((t, i) => t * Convert.ToInt32(Math.Pow(10, array.Length - i - 1)))
.Sum();
回答by ebb
var finalScore = int.Parse(array
.Select(x => x.ToString())
.Aggregate((prev, next) => prev + next));

