有什么方法可以将数字的总和转换回枚举?

时间:2019-08-20 13:20:14

标签: c# api enums calculation

因此,在名为osu的游戏中有一些模组,当我调用api进行任何游戏时,我都会得到模组编号的总和(例如72)。但是,我想不出任何办法将其转换回原始的枚举名称。

我尝试过这种方法:(Mods)enabled_mods,但这只会使数字回退。

     enum Mods
     {
         None = 0,
         NoFail = 1,
         Easy = 2,
         TouchDevice = 4,
         Hidden = 8,
         HardRock = 16,
         SuddenDeath = 32,
         DoubleTime = 64,
         Relax = 128,
         HalfTime = 256,
         Nightcore = 512, // Only set along with DoubleTime. i.e: NC only gives 576
         Flashlight = 1024,
         Autoplay = 2048,
         SpunOut = 4096,
         Relax2 = 8192,    // Autopilot
         Perfect = 16384, // Only set along with SuddenDeath. i.e: PF only gives 16416  
         Key4 = 32768,
         Key5 = 65536,
         Key6 = 131072,
         Key7 = 262144,
         Key8 = 524288,
         FadeIn = 1048576,
         Random = 2097152,
         Cinema = 4194304,
         Target = 8388608,
         Key9 = 16777216,
         KeyCoop = 33554432,
         Key1 = 67108864,
         Key3 = 134217728,
         Key2 = 268435456,
         ScoreV2 = 536870912,
         LastMod = 1073741824,
         KeyMod = Key1 | Key2 | Key3 | Key4 | Key5 | Key6 | Key7 | Key8 | Key9 | KeyCoop,
         FreeModAllowed = NoFail | Easy | Hidden | HardRock | SuddenDeath | Flashlight | FadeIn | Relax | Relax2 | SpunOut | KeyMod,
         ScoreIncreaseMods = Hidden | HardRock | DoubleTime | Flashlight | FadeIn
     }

我想获取已求和的值的名称,例如如果我有72个值,我应该获取HiddenDoubleTime。

1 个答案:

答案 0 :(得分:1)

这是您可以执行此操作的一种方法:

Mods selectedMods = (Mods)72;

var individualMods = Enum
    .GetValues(typeof(Mods))
    .Cast<Mods>()
    .Where(mod => selectedMods.HasFlag(mod) && mod != Mods.None)
    .ToList();