473,654 Members | 3,062 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 49276
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
60825
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
53702
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
57405
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
1624
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
15313
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
8375
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
8290
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8593
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
7306
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...
1
6161
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
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
4149
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
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.