在 C# 中将字符串转换为标志枚举
设备将其限位开关的状态报告为一系列 1 和 0(表示包含“010111110000”的字符串)。这些开关的理想表示是像这样的标志枚举:
[Flags]
public enum SwitchStatus
{
xMin,
xMax,
yMin,
yMax,
aMax,
bMax,
cMax,
unknown4,
unknown3,
unknown2,
unknown1,
unknown0
}
是否可以将字符串表示形式转换为枚举?如果是这样,怎么办?
A device reports status of its limit switches as a series of ones a zeros (meaning a string containing "010111110000"). Ideal representation of these switches would be a flags enum like this:
[Flags]
public enum SwitchStatus
{
xMin,
xMax,
yMin,
yMax,
aMax,
bMax,
cMax,
unknown4,
unknown3,
unknown2,
unknown1,
unknown0
}
Is it possible to convert the string representation to the enum? If so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
Convert.ToInt64(value, 2)
或Convert.ToInt32(value, 2)
这将为您提供 long 或 int,然后只需使用You can use
Convert.ToInt64(value, 2)
orConvert.ToInt32(value, 2)
this will give you either the long or the int, then simply use首先,您必须将“二进制字符串”转换为 int。
您必须声明枚举项及其各自的编号:
最后是映射。您可以从这样的数字中获取枚举:
First you have to convert your "binary string" to int.
You have to have declared your enum items with their respective numbers:
At last, the mapping. You get your enum from the number like this: