English 中文(简体)
我需要 in,将其改为双亲(C#)
原标题:I need to take an int and convert it into its binary notation (C#)
  • 时间:2011-02-21 13:16:09
  •  标签:
  • c#
  • binary

我需要一刀切,把它变成一.。

i.e. I need to take 1 and turn it into 00000001 or 160 and turn it into 10100000

目前,我正在利用这一点。

            int x = 3;
            string s = Convert.ToString(x, 2);
            int b = int.Parse(s);

这是做事的有益方式,因此,我正在寻找更好的办法。

任何建议?

http://www.ohchr.org。

基本上,我需要获得一份不超过256个基数的名单。 我会把所有数字储存在一份清单中,并将这些数字保留在我的台上。

<>>>>>

我决定把基-2号保持为一种扼杀,而不是将其重新分类。 感谢帮助和担忧混淆!

最佳回答

I think you are confusing the data type Integer with its textual representation.

int x = 3;

is the number three regardless of the representation (binary, decimal, hexadecimal, etc.) When you parse the binary textual representation of an integer back to integer you are getting a different number. The framework assumes you are parsing the number represented in the decimal base and gives the corresponding integer. You can try

int x = 1600;
string s = Convert.ToString(x, 2);
int b = int.Parse(s);

and it will throw an exception because the binary representation of 1600 interpreted as decimal is too big to fit in an integer

问题回答

如需要<代码>byte,请看<代码>BitConverter。 GetBytes(方法。 它不会回头.,而是一阵.。

The int is already a binary number. What exactly are you looking to do with the new integer? What you are doing is setting a base 10 number to a base 2 value. That s kind of confusing and I think you must be trying to do something that should happen a different way.

我不知道你们最终需要什么......这可能有助于:

in形形形色体:

byte[] bytes = BitConverter.GetBytes(x);

将斜体改为轨道阵列:

BitArray bitArray = new BitArray(new[] {x});




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签