473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 49205
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.