473,387 Members | 1,530 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,387 software developers and data experts.

Print a number in binary format.

It looked like that I can print a number in hexadecimal format with
Console.Write, but I couldn't find how to print in binary format. I
mean 000...0001 for 1, 000...0010 for 2 and so on.

I wrote a dirty code, and it seemed to be working, but it looks like
quite inefficient. Is there any .NET built in method or a better way?

Below is the code I wrote:
class Program
{
static void Main(string[] args)
{
PrintInBinary(1);
PrintInBinary(1<<2);
}
static void PrintInBinary(int num)
{
byte[] bytes = BitConverter.GetBytes(num);
for (int count = bytes.Length - 1; count >= 0; count--)
{
for (int position = 7; position >= 0; position--)
PrintBitAt(bytes[count], position);
Console.Write(" ");
}
Console.WriteLine();
}
static void PrintBitAt(byte b, int position)
{
if(position>7)
return;
byte a=(byte)Math.Pow(2,position);
if ((b & a) == a)
Console.Write("1");
else
Console.Write("0");
}
}
Oct 15 '08 #1
1 25240
Hi Sin Jeong-hun,

i think thats what you are looking for:
int SomeNumber = 12345;
string NumberBinaryString = Convert.ToString(SomeNumber, 2); //radix 2
MessageBox.Show(NumberBinaryString);
Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Latest Project: http://www.codeplex.com/restarts
Latest Open-Source Projects: http://entwicklung.junetz.de
-----------------------
"This reply is provided as is, without warranty express or implied."
"Sin Jeong-hun" <ty*******@gmail.comschrieb im Newsbeitrag
news:40**********************************@t18g2000 prt.googlegroups.com...
It looked like that I can print a number in hexadecimal format with
Console.Write, but I couldn't find how to print in binary format. I
mean 000...0001 for 1, 000...0010 for 2 and so on.

I wrote a dirty code, and it seemed to be working, but it looks like
quite inefficient. Is there any .NET built in method or a better way?

Below is the code I wrote:
class Program
{
static void Main(string[] args)
{
PrintInBinary(1);
PrintInBinary(1<<2);
}
static void PrintInBinary(int num)
{
byte[] bytes = BitConverter.GetBytes(num);
for (int count = bytes.Length - 1; count >= 0; count--)
{
for (int position = 7; position >= 0; position--)
PrintBitAt(bytes[count], position);
Console.Write(" ");
}
Console.WriteLine();
}
static void PrintBitAt(byte b, int position)
{
if(position>7)
return;
byte a=(byte)Math.Pow(2,position);
if ((b & a) == a)
Console.Write("1");
else
Console.Write("0");
}
}
Oct 15 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rim | last post by:
Hi, >>> print '%x' % 54 36 >>> print '%b' % 54 Traceback (most recent call last): File "<stdin>", line 1, in ? ValueError: unsupported format character 'b' (0x62) at index 1 >>>
4
by: Rusty Shackleford | last post by:
I have a Summer in front of me without any school, and I'd like to add a new format for python print strings that will show any number in a binary representation. For example: >>> '%b' % 3 11...
12
by: neutrino | last post by:
Greetings to the Python gurus, I have a binary file and wish to see the "raw" content of it. So I open it in binary mode, and read one byte at a time to a variable, which will be of the string...
18
by: Bern | last post by:
how to specifiy a binary number in c++? hex numbers are specified by 0x prefix
4
by: ai lian | last post by:
When I use printf to print a large double number, the result is not the same as the original input number. For example: double num=899999999999.894400; printf("%lf\n",num); The output is:...
1
by: Bhanu | last post by:
I have a problem in a piece of C Program. I have to print a number of size 19 digit (No decimals). What data type should I use to store and print this no. I have already tried using double, long...
3
by: G | last post by:
Hi ,you guys: look at the code below //////////////////////////////////////////////////////////////////////////////////////////////// #include<iostream> using namespace std; int main() {
14
by: mosi | last post by:
Problem: how to get binary from integer and vice versa? The simplest way I know is: a = 0100 a 64 but: a = 100 (I want binary number) does not work that way.
12
by: waterdriven | last post by:
Hello; I am a newbie. A homework assignment was assigned and I am having trouble getting started. The assignment reads: Write a program to print out the binary value of a 16 bit number.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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.