(byte)Convert.ToChar(anyStringOfLengthOne) 怎么可能抛出错误?
我们在项目中有这样一段相当简单的代码:
string input = "Any string";
for (int i = 0; i < input.Length; i++)
{
string stringOfLengthOne = input.Substring(i, 1);
byte value = (byte)Convert.ToChar(stringOfLengthOne);
if (value == someValue)
{
// do something
}
}
输入是一个字符串,其中的字符通常从文件中读取,需要根据其字节值进行处理。
不幸的是,我们没有机会逐步调试这个过程,我们只需要有根据地猜测哪种字符串可能会导致
(byte)Convert.ToChar(anyStringOfLengthOne)
上面的代码抛出“算术运算导致溢出”错误。
我的想法是,一旦我有了一个字符串,就应该总是可以 1. 选择一个字符并 2. 将其转换为字节。然而错误还是发生了。
有什么想法、提示吗?或者有人甚至可以提供一个引发此类错误的字符串?
We have this rather simple code in a project:
string input = "Any string";
for (int i = 0; i < input.Length; i++)
{
string stringOfLengthOne = input.Substring(i, 1);
byte value = (byte)Convert.ToChar(stringOfLengthOne);
if (value == someValue)
{
// do something
}
}
The input is a string with characters usually read from a file that need to be processed depending on their byte value.
Unfortunately, we do not have the chance to debug this process step-by-step, we just need to make an educated guess what kind of string could cause
(byte)Convert.ToChar(anyStringOfLengthOne)
in the code above to throw an "Arithmetic operation resulted in an overflow" error.
My thinking is that as soon as I have a string, it should always be possible to 1. pick a char and 2. convert it to a byte. Yet the error occurs.
Any ideas, hints? Or can someone even provide a string that throws this kind of error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.Net 中的字符长度为 16 位(短/超短)。
C# 的默认项目设置意味着强制转换将起作用,并且只会忽略大于 255 的任何字符的高位,即类似于使用
(byte) (c & 0xff)
。但是,如果您使用检查算术,尝试转换大于 255 的 char 将导致 ArithmeticOverflowExcetion。
算术的默认设置可以在项目的构建设置中设置为选中/取消选中。
示例
替代方案
或者,您可以直接比较字符。
例如,要测试一个字符是否为 0-9
您甚至可以将 char 与 int 进行比较
Characters in .Net are 16 bits (short/ushort) in length.
The default project settings for C# means that the cast would work and will just ignore the higher bits for any character that is larger than 255, i.e. like using
(byte) (c & 0xff)
.However, if you are using checked arithmetic, trying to cast a char that is greater than 255 will result in an ArithmeticOverflowExcetion.
The default setting for arithmetic can be set to checked/unchecked in the project's build settings.
Example
Alternative
Alternativly, you could compare the characters directly.
For example to test if a character is 0-9
You can even compare a char to an int
为什么不访问 input[i] 而使用 Substring 和 Convert?
编辑:
哦,哦,抱歉,我错过了。 .NET (Unicode) 中的字符是 16 位,因此如果您使用非英语字符,则无法将字符转换为字节是很合理的。例如,尝试任何希伯来字母。
Why not access input[i] instead of using a Substring and Convert?
EDIT:
Oh, oh, sorry, I missed it. Characters are 16 bit in .NET (Unicode), so it's very reasonable you can't convert a char to a byte if you're using non English characters. Try any Hebrew letter for instance.
来自文档
字节是 8 位,UTF-16 是 16 位,这就是你收到错误的原因。
From docs
Byte is 8 bits, UTF-16 is 16 bits, this is why you get an error.