2012-03-05 173 views
5

我想要学习如何做的是将一个int数组转换为一个int在C#中。如何将int数组转换为int?

但是我想追加int数组中的值。

实施例:

int[] array = {5, 6, 2, 4}; 

将被转换成等于5624.

感谢预先任何帮助一个int。

+3

如果数组中的数字包含一个无法放入'int'的数字,会发生什么? – Jon 2012-03-05 10:08:24

+1

那些不在0 ... 9范围内的数组项呢? – 2012-05-24 08:46:40

回答

13

只需将每个数字乘以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)); 
} 
+2

为了更快一点Convert.ToInt32(Math.Pow(10,array。(int k = 0; k 2012-03-05 10:27:23

2

尝试以下操作:

 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]; 
     } 
+0

它返回'4265' - 这不是期望的结果。 – 2012-03-05 10:16:41

+0

对不起,你是对的,编辑我的答案。 – 2012-03-05 10:20:11

0

这将是容易,如果你理解了十进制系统是如何工作的。

因此,让我解释一下,对于你来说:一个十进制数字包含以十为底的单个数字。

这意味着你必须通过此数组迭代(向后!)和10^

相乘有关示例5624是指:(5×10^3)+(6×10^2)+(2 * 10^1)+(4 * 10^0)

也请考虑:http://en.wikipedia.org/wiki/Horner_scheme

2

使用此代码,你只是想连接你的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 

这不是e xact代码。

+0

-1不是一个好的解决方案,字符串操作很慢。 – 2012-03-05 10:21:36

+3

@ FelixK。 OP没有指定他需要时间敏感的代码,这是最易读的编码答案。 – ediblecode 2012-03-05 10:36:54

+0

尽管如此,Shadow Wizard的答案更具可读性和安全性,但重要的是,与其他解决方案相比,它在速度方面不是一个好的解决方案。 – 2012-03-05 10:43:37

4

另一种简单的方法:这里

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 
} 

诀窍是使阵列的数字串然后解析它作为整数。

+1

最简单! +1。不是最好的.. – nawfal 2012-11-13 15:43:54

+0

@nawfal谢谢,我认为[这一个](http://stackoverflow.com/a/9564827/447356)是最好的,upvoted它自己。 :) – 2012-11-13 16:01:18

+0

我爱你。如果效率不重要,这看起来最酷http://stackoverflow.com/a/9567257/661933 – nawfal 2012-11-13 16:13:02

0

这将做到这一点:

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; 
} 
2
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; 
} 
0

而只是为了好玩...

arr.Select((item, index) => new { Item = item, Power = arr.Length - (index - 1) }).ToList().ForEach(item => total += (int)(Math.Pow(10, item.Power) * item.Item)); 
6
int output = array 
    .Select((t, i) => t * Convert.ToInt32(Math.Pow(10, array.Length - i - 1))) 
    .Sum(); 
+1

+1这很酷 – Richard 2012-03-05 13:17:05

0
var finalScore = int.Parse(array 
    .Select(x => x.ToString()) 
    .Aggregate((prev, next) => prev + next)); 
1

你可以使用字符串流 (包括 “sstream” )

using namespace std; int main(){

int arr[3]={3,2,4};  //your array.. 

stringstream ss; 

ss<<arr[0]; //this can be run as a loop 
ss<<arr[1]; 
ss<<arr[2]; 


int x; 
ss>>x; 

cout<<x;  //simply the int arr[3] will be converted to int x.. 
+0

记得添加** #include ** – 2014-02-22 18:20:46