| | |
Binary formatting an integer
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
Hi all,
I like to write out integers in binary format to the console.
Let's say I want Myint=5 to be written I use Convert.ToString(Myint,2)
I get 101, what I want is 0000000000000101
I found this struct on the net which could do the trick, but is there a better way?
I like to write out integers in binary format to the console.
Let's say I want Myint=5 to be written I use Convert.ToString(Myint,2)
I get 101, what I want is 0000000000000101
I found this struct on the net which could do the trick, but is there a better way?
c# Syntax (Toggle Plain Text)
struct IntBits { private int _bits; public IntBits(int InitialBitValue) { _bits = InitialBitValue; } //declare indexer public bool this[int index] { get { return (_bits & (1 << index)) != 0; } set { if (value) _bits |= (1 << index); //set bit index 1 else _bits &= ~(1 << index); //set bit index 0 } } }
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
You could do it at the string level with
.PadLeft() or are you wanting this done property with binary formatting? c# Syntax (Toggle Plain Text)
private void button2_Click(object sender, EventArgs e) { int myInt = 5; string res = Convert.ToString(myInt, 2).PadLeft(16, '0'); System.Diagnostics.Debugger.Break(); }
Thanks Scott, I was not aware of the existence of the PadLeft function
This will do for what I want(thanks again), but what do you mean by
As you probably know English is not my native language.
This will do for what I want(thanks again), but what do you mean by
•
•
•
•
or are you wanting this done property with binary formatting?
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
I was meaning if there is some way to use an 
It sounds like you're adding binary support to your calculator? When you're done I can replace my TI-85 with your program I think
IFormatProvider for binary to get the string padded with zeros. I don't know of a way but I didn't know if you weren't aware of .PadLeft() or if you didn't want to use it, so that is what I was trying to ask 
It sounds like you're adding binary support to your calculator? When you're done I can replace my TI-85 with your program I think
No I was not planning on doing that.
Did you know there are some subtle differences between the MS calculator and the one I have at home? I am glad I finished it as it is
The reason I needed a binary format is I wanted to see what graphical pattern I would get when using prime numbers. So I would make colored squares depending if a bit is 0 or 1.
I first made a test program to get the zeros and ones, thanks to you I can continue!
Did you know there are some subtle differences between the MS calculator and the one I have at home? I am glad I finished it as it is
The reason I needed a binary format is I wanted to see what graphical pattern I would get when using prime numbers. So I would make colored squares depending if a bit is 0 or 1.
I first made a test program to get the zeros and ones, thanks to you I can continue!
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
I was actually reading that thread you posted about the calculators being different earlier today but I didn't understand it and it was so old I didn't want to post on it. What differences have you noticed since then?
I think you are referring to this post http://www.daniweb.com/forums/thread187538.html
It was while I was testing my C# calculator I came up with this.
I tried out the most wierd keystroke sequences to let it somehow "misbehave". I did not come up with new ones, besides this is a keystroke sequence you normally would not use. Given the fact that I can not try out all the possible keystroke permutations and that my C# calculator works (as far as I know), I left it with that and did not look for new issues.
It was while I was testing my C# calculator I came up with this.
I tried out the most wierd keystroke sequences to let it somehow "misbehave". I did not come up with new ones, besides this is a keystroke sequence you normally would not use. Given the fact that I can not try out all the possible keystroke permutations and that my C# calculator works (as far as I know), I left it with that and did not look for new issues.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Make love, no war. Cave ab homine unius libri.
Danny
1
#9 4 Days Ago
@sknake, i noticed one..if you key in 1+2=-= MS calc will return zero (total - total = 3 - 3 = 0) whilst my Xerox returns -1 (last number - total = 2 - 3 = -1).
I'm also curious as to whether this is truely incorrect key sequenes :p the same way that if you hit enter more than once it re-applies the last calculation, perhaps this is actually a feature
I'm also curious as to whether this is truely incorrect key sequenes :p the same way that if you hit enter more than once it re-applies the last calculation, perhaps this is actually a feature
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
![]() |
Similar Threads
- Fixed-Point Binary to Decimal conversion (C++)
- Binary (base 2) to Decimal (base 10) HELP! (C)
- Behaviour of Binary Search Program (C)
- Binary String in to Integer in c++ (C++)
- txt data into 2d Array (C++)
- How to convert a decimal number into its binary equivalent (Python)
- Making a .dat file (C++)
- can't proceed from here. (C++)
- Binary Numbers (Computer Science)
Other Threads in the C# Forum
- Previous Thread: displaying two strings of ascii conversion
- Next Thread: Need Help with this please anybody help me
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener login mandelbrot math mouseclick mysql networking object operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






