Wednesday, December 19, 2012

C# Convert Decimal to Binary and Vice Versa

Decimal to Binary conversion as below:
 
int DecimalVal = 256;
string BinaryVal = Convert.ToString(DecimalVal, 2);
Binary to Decimal conversion as below:
 
string BinaryVal = "01001101";
string DecimalVal = Convert.ToInt32(BinaryVal, 2).ToString();

2 comments:

  1. is it possible to store converted decimal value into integer ?

    ReplyDelete
    Replies
    1. it up to you. what are you looking for?
      if 1234.56 and you only want 1234 you can use Convert.ToInt32
      if 1234.56 and you want 1235 you can use Math.Round

      Delete