473,503 Members | 12,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 49229
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4293
by: CSharpener | last post by:
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...
7
60816
by: Wilfried Mestdagh | last post by:
Hi, how to typecast or convert a byte to a string and the way arount ? I have string from a textbox and wants to transmit it trough a socket. witch accepts a byte. Now I make a copy from the...
6
53675
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
7
57388
by: EOS | last post by:
Hi, I would like to ask on how to convert array of bytes, Byte into String? I tried StringIwant = System.Convert.ToString(byteData); but it actually return "System.Byte" rather than convert...
2
1618
by: Peter Schmitz | last post by:
Hi, how can I convert a 1-dimensional array of type byte() to string under VB.net? Greetings, Peter
3
15096
by: shadabahmad | last post by:
Hi All! I had tried to search many forums to get this code. Yes, just for a single line code. But when everything else failed, I decided to write my own. And it is really working good. ...
0
7193
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7067
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4992
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.