How to Convert to Binary

Madz

Centurion
Joined
Jan 22, 2003
Messages
155
Can any one tell me some method throgh which i can convert a deciaml to Binary

Such as if i want to convert 256 to 010101 format

what whould be the keywork of C#.
 
I'm not aware of any methods that can easily fulfill your requirements (similar to javascript's int.toString(2) functionality) but here's a little C# function that can get you started:

private string ToBinaryString(int num, int stringLength)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(stringLength);
for (int k = 0; k < stringLength; k++){
sb.Append((num & (int)Math.Pow(2, k)) > 0 ? "1" : "0");
}
return sb.ToString();
}
 
Back
Top