Friday, March 25, 2011

Two important C# code snippets

Hi everyone!
And this time especially the AX-team of our little contest!

Here are the two last APXD-snippets in C# :

Converting from a String like '10001000' to an int (this would be 136):

int getint(String bin) {
  int result = 0;
  for(int i = 0; i< 8; i++){
   if(bin[i] == '1'){
    result +=(int) Math.Pow (2,7-i);
   }
  }
  return result;
}



Converting from an int like 136 to the binary-String:

String getbin(int input) {
   String result = "";
   int ax;
   for (int i = 0; i < 8; i++) {
    ax = (int)Math.Pow(2, 7 - i);
    if (input >= ax) {
     result += "1";
     input -=ax;
    }
    else {
     result += "0";
    } 
   }
  return result;
}


That's all for the moment!
Have a good weekend!

apexys

No comments:

Post a Comment