无法将特殊字符串转换为整数 [英] Can't convert special string to integer
问题描述
Hi
如何将以下字符串转换为整数或普通ASCII字符串
string input =3285 ; //3285不等于3285
int no = Convert.ToInt32(输入);
它会抛出异常< br $>
谢谢
我尝试了什么:
string input =3285; //3285不等于3285
int no = Convert.ToInt32(输入);
它会抛出异常
Hi
How to convert the following string to integer or to normal ASCII string
string input = "3285"; //"3285" not equal to "3285"
int no = Convert.ToInt32(input);
It will threw exception
Thanks
What I have tried:
string input = "3285"; //"3285" not equal to "3285"
int no = Convert.ToInt32(input);
It will threw exception
推荐答案
当然它会抛出异常。字符串中的每个数字之间都有空格。首先删除空格,然后使用int.TryParse进行转换。
Of course it will throw an exception. You have white space between each digit in the string. First remove the white space then use the int.TryParse to do your convertions.
/ /3285不等于3285
//"3285" not equal to "3285"
全部在注释中,第二个3285是由数字组成的整数,但是一个是一些看起来像数字但不是数字的unicode。
3用utf-8编码为EF BC 93
2编码为utf-8为EF BC 92
8在utf-8中编码为EF BC 98
5在utf-8中编码为EF BC 95
3也被解码为U + FF13 FULLWIDTH DIGIT THREE,这不是C#视角的数字。
如果这是典型的数据你必须处理,你应该首先将unicode字符转换为传统数字。
All is in the comment, the second 3285 is an integer made of digits, but the one is some unicode that look like digits but is not made of digits.
the "3" is coded in utf-8 as EF BC 93
the "2" is coded in utf-8 as EF BC 92
the "8" is coded in utf-8 as EF BC 98
the "5" is coded in utf-8 as EF BC 95
the "3" is also decoded as U+FF13 FULLWIDTH DIGIT THREE, which is not a digit from the C# point of view.
If this is typical of data you have to handle, you should convert unicode chars to traditional digits first.
这篇关于无法将特殊字符串转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!