转换为八进制时出现奇怪的 FormatException
Strange FormatException when converting to octal
所以我有一个整数数字208
我不希望很多人理解为什么我这样做,但我想要做的最终结果是得到八进制数 208(二零八)的以 10 为底的表示形式。我希望令人困惑的事情(对于将尝试回答这个问题的人来说)是虽然 208 是一个整数,但我更像是一个包含字符二、零和八的字符串来使用它。 如果对此还有其他问题,请告诉我,因为我认为这会引起一些混乱。
无论如何,我是这样做的,以 10 进制表示“208”:
- 将
int
208 转换为 string
"208".
- 取
string
"208",并从 octal 解析为 decimal.
那么,对应的源码如下:
public byte OctalToDecimal(int octalDigits)
{
byte decimalValue = 0;
string octalString = string.Empty;
// first, get a string representation of the integer number
octalString = octalDigits.ToString();
// now, get the decimal value of the octal string
decimalValue = Convert.ToByte(octalString, 8);
// set the decimal-value as the label
return decimalValue;
}
我在 octalDigits = 208
时收到格式异常。我收到一条消息,说 octalString
的值中有其他字符。为什么会这样?我所做的只是从 int
转换为 string
,它非常 short/simple,不像我在上面附加任何东西。这是怎么回事?
你应该知道八进制数字的数字范围是 0 到 7
此处,一些有用的链接
the octal representations of bytes range from 000 to 377?
八进制数不能包含数字 8,例如以 10 进制表示不能包含 "digit" 10,二进制不能包含数字 2。
所以我有一个整数数字208
我不希望很多人理解为什么我这样做,但我想要做的最终结果是得到八进制数 208(二零八)的以 10 为底的表示形式。我希望令人困惑的事情(对于将尝试回答这个问题的人来说)是虽然 208 是一个整数,但我更像是一个包含字符二、零和八的字符串来使用它。 如果对此还有其他问题,请告诉我,因为我认为这会引起一些混乱。
无论如何,我是这样做的,以 10 进制表示“208”:
- 将
int
208 转换为string
"208". - 取
string
"208",并从 octal 解析为 decimal.
那么,对应的源码如下:
public byte OctalToDecimal(int octalDigits)
{
byte decimalValue = 0;
string octalString = string.Empty;
// first, get a string representation of the integer number
octalString = octalDigits.ToString();
// now, get the decimal value of the octal string
decimalValue = Convert.ToByte(octalString, 8);
// set the decimal-value as the label
return decimalValue;
}
我在 octalDigits = 208
时收到格式异常。我收到一条消息,说 octalString
的值中有其他字符。为什么会这样?我所做的只是从 int
转换为 string
,它非常 short/simple,不像我在上面附加任何东西。这是怎么回事?
你应该知道八进制数字的数字范围是 0 到 7
此处,一些有用的链接
the octal representations of bytes range from 000 to 377?
八进制数不能包含数字 8,例如以 10 进制表示不能包含 "digit" 10,二进制不能包含数字 2。