Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Convert Integer value to Binary value as a string in C#?

Tags:

string

c#

Not sure if I worded the title correctly, so my apologies.

I want to simply convert an integer to strings that hold the binary representation of that integer.

Example:
116 would convert to "1110100"

Is there anything built into .Net or will I need to write a small parsing algorithm?

like image 496
Jeff LaFay Avatar asked Dec 12 '22 18:12

Jeff LaFay


1 Answers

This will do it:

// bin = "1110100"
var bin = Convert.ToString(116, 2)

The second parameter specifies which base to convert to.

like image 151
alexn Avatar answered Apr 05 '23 23:04

alexn