C#中如何将字符串转换为int数组

发布于 2024-12-07 14:03:14 字数 503 浏览 1 评论 0原文

我正在制作一个信用卡验证器程序,其中我要求一个 16 位数字(信用卡号)的字符串,并且我想将其转换为 int 数组。我该怎么做?然后我需要将从第一个数字开始的所有其他数字乘以 2。

char[] creditNumbers = creditCardNumber.ToCharArray();

creditNumbers[0] = (char)((int)(creditNumbers[0] * 2));
creditNumbers[2] = (char)((int)(creditNumbers[2] * 2));
creditNumbers[4] = (char)((int)(creditNumbers[4] * 2));
creditNumbers[6] = (char)((int)(creditNumbers[6] * 2));
creditNumbers[8] = (char)((int)(creditNumbers[8] * 2));

这是我到目前为止所做的,但我的转换没有正确完成。我该如何解决这个问题?

I am making a credit card validator program in which I am asking for a string which is going to be a 16 digit number (credit card #) and I want to convert that into an int array. How do I do that? I need to then multiply every other digit starting from the first digit by 2.

char[] creditNumbers = creditCardNumber.ToCharArray();

creditNumbers[0] = (char)((int)(creditNumbers[0] * 2));
creditNumbers[2] = (char)((int)(creditNumbers[2] * 2));
creditNumbers[4] = (char)((int)(creditNumbers[4] * 2));
creditNumbers[6] = (char)((int)(creditNumbers[6] * 2));
creditNumbers[8] = (char)((int)(creditNumbers[8] * 2));

This is what I made so far but my casting is not done properly. How do I fix the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一曲琵琶半遮面シ 2024-12-14 14:03:14

要获取 int 数组,我会这样做:

creditCardNumber.Select(c => int.Parse(c.ToString())).ToArray()

使用 检查数字Luhn算法,你可以这样做:

bool IsValid(string creditCardNumber)
{
    var sum = creditCardNumber.Reverse()
                              .Select(TransformDigit)
                              .Sum();

    return sum % 10 == 0;
}

int TransformDigit(char digitChar, int position)
{
    int digit = int.Parse(digitChar.ToString());
    if (position % 2 == 1)
        digit *= 2;

    return digit % 10 + digit / 10;
}

To just get the int array, I would do this:

creditCardNumber.Select(c => int.Parse(c.ToString())).ToArray()

To check the number using the Luhn algorithm, you could do this:

bool IsValid(string creditCardNumber)
{
    var sum = creditCardNumber.Reverse()
                              .Select(TransformDigit)
                              .Sum();

    return sum % 10 == 0;
}

int TransformDigit(char digitChar, int position)
{
    int digit = int.Parse(digitChar.ToString());
    if (position % 2 == 1)
        digit *= 2;

    return digit % 10 + digit / 10;
}
别想她 2024-12-14 14:03:14
var intArray = creditCardNumber.ToCharArray().Select(o => int.Parse(o.ToString())).ToArray();
var result = new List<int>();

for(int i=0; i<intArray.Length; i++){

    if((i % 2) == 0)
      result.Add(intArray[i] * 2);
    else
        result.Add(intArray[i]);
}

Console.Write(string.Concat(result.Select(o => o.ToString())));
var intArray = creditCardNumber.ToCharArray().Select(o => int.Parse(o.ToString())).ToArray();
var result = new List<int>();

for(int i=0; i<intArray.Length; i++){

    if((i % 2) == 0)
      result.Add(intArray[i] * 2);
    else
        result.Add(intArray[i]);
}

Console.Write(string.Concat(result.Select(o => o.ToString())));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文