Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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#.

The one and only

Dr. Madz

eee-m@il

Posted

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();

}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...