C#中如何将字符串转换为int数组
我正在制作一个信用卡验证器程序,其中我要求一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要获取
int
数组,我会这样做:使用 检查数字Luhn算法,你可以这样做:
To just get the
int
array, I would do this:To check the number using the Luhn algorithm, you could do this: