简体   繁体   English

是否有针对此错误的解决方案,将小数转换为二进制

[英]Is there a solution for this error for converting decimal to binary

I want to convert a decimal number ( int ) to a binary number and print it out. 我想将十进制数( int )转换为二进制数并打印出来。 My code in C# is this: 我在C#中的代码是这样的:

    int t1 = 2;
    public string test = Convert.ToString(t1, 2);

I get the error: 我得到错误:

A field initializer cannot reference the nonstatic field, method, or property 'field' 字段初始化程序无法引用非静态字段,方法或属性“字段”

I cannot find a solution for this :/ 我找不到解决方案:/

I found the solution: you have to declare t1 as static. 我找到了解决方案:您必须将t1声明为static。 static int t1 works! 静态INT T1的作品!

Check this code, 检查此代码,

using System;

public class Program
{
    public static void Main()
    {
        int static t1 = 2;
        string test = Convert.ToString(t1, 2);
        Console.WriteLine(test);
    }
}

Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM