• Welcome to Valhalla Legends Archive.
 

char -> hex and hex -> char

Started by Sargera, July 08, 2004, 02:22 AM

Previous topic - Next topic

Sargera

I'm currently developing DynamicBot.  The bot uses a bitmask to check for user flags.  Users obviously cannot interpret the bitmask in a meaningful way, so you use a flagstring so-to-speak like "ABCD."  My question is how do I convert each character (flagstring) into the integral-type hex equivolent?  I've tried a few methods, neither of them worked, so that is why I am resorting to these boards.

Bottom line: I need to know how to convert a character ('A' 'B' 'C') into the integral-hex equivolent flag that the bot can understand as it uses a bitmask to check flags.

My constants are declared as the following as this will certainly effect the solution to the problem:
#define FLAG_A 0x01
#define FLAG_B 0x02
#define FLAG_C 0x04
#define FLAG_D 0x08
#define FLAG_E 0x10
#define FLAG_F 0x20
#define FLAG_G 0x40
#define FLAG_H 0x80
#define FLAG_I 0x100
#define FLAG_J 0x200
#define FLAG_K 0x400
#define FLAG_L 0x800
#define FLAG_M 0x1000
#define FLAG_N 0x2000
#define FLAG_O 0x4000
#define FLAG_P 0x8000
#define FLAG_Q 0x10000
#define FLAG_R 0x20000
#define FLAG_S 0x40000
#define FLAG_T 0x80000
#define FLAG_U 0x100000
#define FLAG_V 0x200000
#define FLAG_W 0x400000
#define FLAG_X 0x800000
#define FLAG_Y 0x1000000
#define FLAG_Z 0x2000000

Moonshine

Unless I interpretted what you're wanting incorrectly, I don't see what the big deal is...


In a loop...

switch (toupper(flagstring[i])) {
   case 'A': HandleFlag(FLAG_A); break;
   // ...
   case 'Z': HandleFlag(FLAG_Z); break;
   default: break;
}


Adron

1 << flagstring - 'A'

Sargera

I originally had something similar to that, Adron.  I'll see if your version works.

What I had before:
           if (pm == '+') {
               for (int i = 0; i < sizeof(flags); ++i) {
                   char cCharFlag = flags[i];
                   cCharFlag = cCharFlag - 'A';
                   tuser_flags |= tuser_flags | (1 << cCharFlag);
               }
               rUserMap[tuser].flags = tuser_flags;
               for (int i = 0; i <= sizeof(flags); ++i) {
                   flags[i] = '\0';
               }
               for (int i = 0; i < 26; ++i) {
                   if (tuser_flags & (1 << i)) {
                       flags += (i + 'A');
                   }
               }


Note: That code was taken from my moduser command function.

Sargera

Quote from: Adron on July 08, 2004, 09:06 AM
1 << flagstring - 'A'

Your solution worked successfully, thanks.  One last question, while I've tried a few ways to accomplish this, I haven't successfully been able to, how do I convert the now hex flags into a flagstring the users can understand?

Adron


char flagstring['Z' - 'A' + 2];
char *p = flagstring;
for(int bit = 0; bit <= 'Z' - 'A'; bit++)
  if(flags & (1 << bit)) *p++ = bit + 'A';
*p = 0;

Sargera

Also, instead of adding (ORing) how would I subtract flags?  I'd assume taking the AND of the complemented value?  I've tried, and it comes to no avail.

Adron

flags &= ~(1 << flagstring - 'A');

Maddox

#8
I thought this quite neat. I think Skywing used this for BinaryChat or ZeroBot.


// User attribute data type (32 bits)
union UserAttributes {
   struct {
      // Privileges
      unsigned A : 1;
      unsigned B : 1;
      unsigned C : 1;
      unsigned D : 1;
      unsigned E : 1;
      unsigned F : 1;
      unsigned G : 1;
      unsigned H : 1;
      unsigned I : 1;
      unsigned J : 1;
      unsigned K : 1;
      unsigned L : 1;
      unsigned M : 1;
      unsigned N : 1;
      unsigned O : 1;
      unsigned P : 1;
      unsigned Q : 1;
      unsigned R : 1;
      unsigned S : 1;
      unsigned T : 1;
      unsigned U : 1;
      unsigned V : 1;
      unsigned W : 1;
      unsigned X : 1;
      unsigned Y : 1;
      unsigned Z : 1; // 26
      // Other information
      unsigned Designated : 1;
      unsigned Operator : 1; // 28
      unsigned Reserved : 4; // 32
   };
   struct {
      unsigned Attributes : 26; // 26
      unsigned Information : 6; // 32
   };
   inline void Set(unsigned char cAttribute)
   {
      if(cAttribute < 'A' || cAttribute > 'Z')
         return;
      dwFlags |= (1 << (cAttribute - 'A'));
   }
   inline void Set(UserAttributes _Attributes)
   {
      Attributes |= _Attributes.Attributes;
   }
   inline void Remove(unsigned char cAttribute)
   {
      if(cAttribute < 'A' || cAttribute > 'Z')
         return;
      dwFlags &= ~(1 << (cAttribute - 'A'));
   }
   inline void Remove(UserAttributes _Attributes)
   {
      Attributes &= ~_Attributes.Attributes;
   }
   inline bool Check(unsigned char cAttribute) const
   {
      if(cAttribute < 'A' || cAttribute > 'Z')
         return false;
      return !!(dwFlags & (1 << (cAttribute - 'A'))); // ~~~ not 'a' gddmit ~~~
   }
   inline bool Check(UserAttributes _Attributes) const
   {
      return (Attributes & _Attributes.Attributes) == _Attributes.Attributes;
   }
   inline void Replace(UserAttributes _Attributes, bool bReplaceInformation)
   {
      if(bReplaceInformation)
         dwFlags = _Attributes.dwFlags;
      else
         Attributes = _Attributes.Attributes;
   }
   DWORD dwFlags; // 32
};
asdf.