473,837 Members | 1,574 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:(0xF E).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 49323
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.ToStrin g(integerValue, 2);

Regards,

Joe

"CSharpener " <cs********@sof thome.net> wrote in message
news:9B******** *************** ***********@mic rosoft.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:(0xF E).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********@sof thome.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:(0xF E).toString(2)

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


Convert.ToStrin g (byte, int)

eg

byte b = 231;

string s = Convert.ToStrin g(b, 2);

// s is now "11100111"

--
Jon Skeet - <sk***@pobox.co m>
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.ToStrin g(i, base); //base = 2, 8,10 or 16

HTH

Raj

"CSharpener " <cs********@sof thome.net> wrote in message
news:9B******** *************** ***********@mic rosoft.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:(0xF E).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 javascript:(0xFE).toString(2 No problem. So, how do I do the same in C#? What simple thing am I missing
7
60832
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 string first, but I'm sure there is some other way. For receiving I have to display the received string into a textbox. same question ? -- rgds, Wilfried
6
53735
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 do I convert it to: dim myStr as string = "11,22,33,44"
7
57422
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 the data I wanted into string. I guess it should be a rather simple question.
2
1630
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
15697
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. Private Function Byte2Str(ByVal gByte() As Byte) As String Dim X As Integer Dim gTmp As String = "" For X = 0 To gByte.Length - 1
0
9837
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10563
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10621
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10267
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9392
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6998
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5668
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5845
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4474
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 we have to send another system

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.