0

I don't know what the right way to do this. But this is what I want. I have an Enum

EValue{A=1,B=2,C=4,D=8,E=16,}

I need to say int value in database as number ,

say if select A,c,E

need to say 1=4=16 =21 in database.

Which is ok but then from 21 how to I retrieve a,c,e

Thanks for help in advance

DS2020
  • 279
  • 1
  • 4
  • 20
  • Do you have an application sitting on top of this database? Which language? You're describing a "flags" enum and most languages have easy ways to do what you're asking. – Cᴏʀʏ Sep 05 '14 at 11:54

2 Answers2

0

You could do something like this (pseudo-code, since you didn't say what language you're using):

function getEnums(value:string):enum[]
{
    var result = new enum[];
    foreach (var e in Enum.getValues(enum).orderBy(val.toInt()))
    {
        if (value == 0)
            break;

        var eVal = e.toInt();
        if (value >= eVal)
        {
            result.add(e);
            value -= eVal;
        }        
    }
    return result;
}
Florian Gl
  • 5,984
  • 2
  • 17
  • 30
0

Looks like your enum values can be represented by powers of 2.

e.g. 2^0 = 1, 2^1 = 2 .. etc.

So your problem comes down to : Convert decimal to bianry.

If you put the simple logic around that, you could get the respective enums.

e.g. For input 21, you could get 1, 4, 16 ( = 2 power 0, 2, 4) 

now you can map them to ENUMs.

Snehal Masne
  • 3,403
  • 3
  • 31
  • 51