469,645 Members | 1,127 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

HOWTO Convert Byte or int to Binary String in C#?

This should be *so* easy! How do I convert a Byte or int to a binary string representation in C#

In JavaScript, it goes like this for an int:

javascript:(123).toString(2
or
javascript:(0xFE).toString(2

No problem. So, how do I do the same in C#? What simple thing am I missing

CSharpene

Nov 16 '05 #1
4 48680
Greets,

You can use the ToString() method of the Convert class to convert the
value to a string in a different base. The base should be either 2, 8, 10
or 16 for binary, octal, decimal and hexadecimal, respectively:
string bits = Convert.ToString(integerValue,2);

Regards,

Joe

"CSharpener" <cs********@softhome.net> wrote in message
news:9B**********************************@microsof t.com...
This should be *so* easy! How do I convert a Byte or int to a binary string representation in C#?
In JavaScript, it goes like this for an int:

javascript:(123).toString(2)
or
javascript:(0xFE).toString(2)

No problem. So, how do I do the same in C#? What simple thing am I missing?
CSharpener

Nov 16 '05 #2
CSharpener <cs********@softhome.net> wrote:
This should be *so* easy! How do I convert a Byte or int to a binary
string representation in C#?

In JavaScript, it goes like this for an int:

javascript:(123).toString(2)
or
javascript:(0xFE).toString(2)

No problem. So, how do I do the same in C#? What simple thing am I
missing?


Convert.ToString (byte, int)

eg

byte b = 231;

string s = Convert.ToString(b, 2);

// s is now "11100111"

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
I knew it had to be that easy and even looked at Convert but missed the obvious

Case closed

Thanks

John
Nov 16 '05 #4

int i = 100;
string str = Convert.ToString(i, base); //base = 2, 8,10 or 16

HTH

Raj

"CSharpener" <cs********@softhome.net> wrote in message
news:9B**********************************@microsof t.com...
This should be *so* easy! How do I convert a Byte or int to a binary string representation in C#?
In JavaScript, it goes like this for an int:

javascript:(123).toString(2)
or
javascript:(0xFE).toString(2)

No problem. So, how do I do the same in C#? What simple thing am I missing?
CSharpener

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Wilfried Mestdagh | last post: by
7 posts views Thread by EOS | last post: by
2 posts views Thread by Peter Schmitz | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.