Friday, January 16, 2009

将字符串转换成整数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChangeStringToInt
{
class Program
{
static void Main(string[] args)
{
string str = "abcdef";
Console.WriteLine( ChangeToInt(str));
}

public static int ChangeToInt(string str)
{
int tot = 0;
char[] cStr;
cStr = str.ToCharArray();
for (int i = 0; i <= cStr.GetUpperBound(0); i++)
{
tot += (int)cStr[i];
}

return tot / cStr.GetUpperBound(0);
}
}
}

No comments: