将数字转换为二进制 [英] Converting a number to binary

查看:142
本文介绍了将数字转换为二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这非常烦人,很久以前我需要将整数转换为二进制值以便上传到设备。工作得很好,不见了。可悲的是我现在需要将一个整数转换为二进制值。我真的需要这样做









This is very annoying, a long time ago I needed to convert an integer to a binary value for uploading to a device. Worked it out good and gone. The sad thing is I now need to convert an integer to a binary value for something else. I really need to get this going




String s = "05";
  int b = 0;

   StringBuilder binary = new StringBuilder();
   for (int j = 0; j <= binary.Length; j++ )
   {
       int val = b;
       for (int i = 0; i < 8; i++)
       {
           binary.Append((val & 128) == 0 ? 0 : 1);
           val <<= 1;
       }
       binary.Append(' ');
   }
 MessageBox.Show("'" + s + "' to binary: " + binary);



我希望5结束为101,将其添加到00000101我可以将零附加到字符串。

如果我运行代码它会锁定


from that I want 5 to end up as 101, to get it to 00000101 I can append the zeros to the string.
If I run the code it locks up

Quote:

Serial_Number.Form1。 button3_Click

(object sender = {Text =无法计算表达式,因为当前方法的代码已经过优化。},

System.EventArgs e = {X = 46 Y = 6按钮=左})第77行+ 0x40字节C#

Serial_Number.Form1.button3_Click
(object sender = {Text = Cannot evaluate expression because the code of the current method is optimized.},
System.EventArgs e = {X = 46 Y = 6 Button = Left}) Line 77 + 0x40 bytes C#



并且调试器返回我做错了什么,我开始认为我需要更深入的路径通过我的系统代码。

格伦


and the debugger returns what am I doing wrong, I am starting to think I need a much deeper route through my system for the code.
Glenn

推荐答案

我不确定你想要外环做什么,但它看起来是无限的。



我已经评论了代码来解释原因:

I''m not sure what you intended the outer loop should do, but it appears to be infinite.

I''ve commented the code to explain why:
StringBuilder binary = new StringBuilder();
// Initial length of the StringBuilder is 0
// The loop will be entered as the condition is less than or equal to 0
for (int j = 0; j <= binary.Length; j++ )
  {
     int val = b;
     for (int i = 0; i < 8; i++)
       {
          binary.Append((val & 128) == 0 ? 0 : 1);
          val <<= 1;
       }
       binary.Append(' ');
       // StringBuilder Length has been increased by 9 characters
       // so the loop continuation condition is still true
  }





我一直使用System.Convert.ToString(Int32值,Int32 base)来获取''binary''字符串。它非常简单:



I''ve always used System.Convert.ToString(Int32 value, Int32 base) to get ''binary'' strings. It''s very straightforward:

// Assuming input is 8 bit
String s = Convert.ToString(5, 2);
// s = "101";
String result = s.PadLeft(8, '0');
// result = "00000101"


请停止它。一个数字已经是二进制的,除非它真的是一个数字,而不是一个数字的字符串表示。

这是非常基本的理解,你需要先学习基础知识。



是的,我明白你想要什么,一些字符串数据为001010101 ......。这是一个字符串!不要混用二进制,实际上就是......所有东西。



-SA
Please stop it. A number is already binary, unless it''s really a number, not a string representation of a number.
This is the very basic understanding, you need to learn basics first.

Yes, I understand what did you want, some string data as "001010101…". This is a string! Don''t mix is with binary, which is… everything, in fact.

—SA


如何(.NET 3.5,使用System.Linq; ):

How about (.NET 3.5, using System.Linq;):
static string Bits(int v)
{
    return BitConverter
             .GetBytes(v)
             .Reverse()
             .Aggregate("", (s, b) => s + Convert.ToString(b, 2).PadLeft(8, '0'));
}
static string Bits(float v)
{
    return BitConverter
             .GetBytes(v)
             .Reverse()
             .Aggregate("", (s, b) => s + Convert.ToString(b, 2).PadLeft(8, '0'));
}
etc.





您可以使用Reverse()来获取所需的字节顺序。

干杯

Andi



You may play with the Reverse() to get the byte order you want.
Cheers
Andi


这篇关于将数字转换为二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆