Announcement

Collapse
No announcement yet.

Bitmasking for C#

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bitmasking for C#

    Referring to the scripts that handles the bitmasking for the dnsbl.tornevall.org-scripts, this is how to make the same routine in C-Sharp. Based on the Hashtable-namespace to get it as similar as possible.

    Code:
    [FONT=Courier New][COLOR=blue]public[/COLOR] [COLOR=#2b91af]Hashtable[/COLOR] BitMask([COLOR=blue]int[/COLOR] MaskVal, [COLOR=blue]params[/COLOR]  [COLOR=blue]int[/COLOR][] bitcheck)[COLOR=black][/COLOR][/FONT]
     [FONT=Courier New]             {[/FONT]
     [FONT=Courier New]                [COLOR=#2b91af]Hashtable[/COLOR] ReturnThis = [COLOR=blue]new[/COLOR] [COLOR=#2b91af]Hashtable[/COLOR]();[/FONT]
     [FONT=Courier New]                [COLOR=#2b91af]Hashtable[/COLOR] Arr = [COLOR=blue]new[/COLOR] [COLOR=#2b91af]Hashtable[/COLOR]();[COLOR=black][/COLOR][/FONT]
     [FONT=Courier New]                [COLOR=blue]int[/COLOR] loadbits = 8;[COLOR=black][/COLOR][/FONT]
     [FONT=Courier New]                [COLOR=blue]try[/COLOR] { loadbits = bitcheck[0]; } [COLOR=blue]catch[/COLOR] ([COLOR=#2b91af]Exception[/COLOR])  { }[COLOR=black][/COLOR][/FONT]
    
     [FONT=Courier New]                [COLOR=green]/*[COLOR=black][/COLOR][/COLOR][/FONT]
     [COLOR=green][FONT=Courier New]                  * PHP SOURCE[COLOR=black][/COLOR][/FONT][/COLOR]
     [COLOR=green][FONT=Courier New]                  * [COLOR=black][/COLOR][/FONT][/COLOR]
     [COLOR=green][FONT=Courier New]                  *   function bitmask ($bit = '', $loadbits = 8)[COLOR=black][/COLOR][/FONT][/COLOR]
     [COLOR=green][FONT=Courier New]*    {[/FONT][/COLOR]
     [COLOR=green][FONT=Courier New]                  *       for ($i = 0 ; $i < $loadbits ; ++$i) {$arr[] = pow(2,$i); }     //  Automatisera bitvärden[COLOR=black][/COLOR][/FONT][/COLOR]
     [COLOR=green][FONT=Courier New]                  *       for ($i = 0 ; $i < count($arr) ; ++$i) {$mask[$i] = ($bit &  $arr[$i]) ? '1' : '0';}     // Sätt 1 till de bitvärden som är  påslagna[COLOR=black][/COLOR][/FONT][/COLOR]
     [COLOR=green][FONT=Courier New]                  *       return $mask;[COLOR=black][/COLOR][/FONT][/COLOR]
     [COLOR=green][FONT=Courier New]                  *   }[COLOR=black][/COLOR][/FONT][/COLOR]
     [COLOR=green][FONT=Courier New]                  */[COLOR=black][/COLOR][/FONT][/COLOR]
    
     [FONT=Courier New]                [COLOR=blue]for[/COLOR] ([COLOR=blue]int[/COLOR] i = 0 ; i  < loadbits ; ++i) {Arr.Add(i.ToString(), [COLOR=#2b91af]Math[/COLOR].Pow(2, i).ToString());}     [COLOR=green]// Automatisera bitvärden[COLOR=black][/COLOR][/COLOR][/FONT]
     [COLOR=blue][FONT=Courier New]for[/FONT][/COLOR][FONT=Courier New] ([COLOR=blue]int[/COLOR] i = 0; i < Arr.Count; ++i)  {ReturnThis.Add(i.ToString(), [COLOR=#2b91af]Convert[/COLOR].ToInt32([COLOR=#2b91af]Convert[/COLOR].ToBoolean(MaskVal & [COLOR=#2b91af]Convert[/COLOR].ToInt32(Arr[i.ToString()].ToString()))).ToString());}[COLOR=black][/COLOR][/FONT]
     [COLOR=blue][FONT=Courier New]return[/FONT][/COLOR][FONT=Courier New]  ReturnThis;[COLOR=black][/COLOR][/FONT]
     [FONT=Courier New]             }[COLOR=black][/COLOR][/FONT]
    -
Sorry, you are not authorized to view this page
Working...
X