将 Binary 转换为 int 而不使用 Convert.ToInt32

发布于 2024-10-05 01:26:52 字数 577 浏览 4 评论 0原文

我需要在 C# 中将二进制 10100101 转换为整数,而不使用 Convert.ToInt64(bin,2),我正在使用 .net 微框架。当我使用 int i = Convert.ToInt32(byt, 2); 时,会抛出异常,并显示以下相当无用的消息:

 #### Exception System.ArgumentException - 0x00000000 (1) ####
    #### Message: 
    #### System.Convert::ToInt32 [IP: 000d] ####
    #### TRNG.Program::loop [IP: 0047] ####
    #### TRNG.Program::Main [IP: 0011] ####
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

I need to convert binary 10100101 to an integer in C# without using Convert.ToInt64(bin,2), I'm working with the .net micro framework. When I use int i = Convert.ToInt32(byt, 2); an exception is thrown with the rather unhelpfull message of:

 #### Exception System.ArgumentException - 0x00000000 (1) ####
    #### Message: 
    #### System.Convert::ToInt32 [IP: 000d] ####
    #### TRNG.Program::loop [IP: 0047] ####
    #### TRNG.Program::Main [IP: 0011] ####
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心不设防 2024-10-12 01:26:52

比 Femaref 的选项稍快,因为它不需要讨厌的方法调用,并且使用 OR 而不是 ADD 只是为了好玩:

public static int ParseBinary(string input)
{
    // Count up instead of down - it doesn't matter which way you do it
    int output = 0;
    for (int i = 0; i < input.Length; i++)
    {
        if (input[i] == '1')
        {
            output |= 1 << (input.Length - i - 1);
        }
    }
    return output;
}

您可能需要:

  • 检查长度是否小于 32 以
  • 检查每个字符是否为 '0 ' 或 '1'

只是为了哈哈,这里有一个 LINQ 版本:

public static int ParseBinary(string input)
{
    return input.Select((c, index) => new { c, index })
        .Aggregate(0, (current, z) => current | (z.c - '0') << 
                                        (input.Length - z.index - 1));
}

或者更简洁:

public static int ParseBinary(string input)
{
    return return input.Aggregate(0, (current, c) => (current << 1) | (c - '0'));
}

Slightly faster than Femaref's option, as it doesn't require a pesky method call, and uses OR instead of ADD just for fun:

public static int ParseBinary(string input)
{
    // Count up instead of down - it doesn't matter which way you do it
    int output = 0;
    for (int i = 0; i < input.Length; i++)
    {
        if (input[i] == '1')
        {
            output |= 1 << (input.Length - i - 1);
        }
    }
    return output;
}

You may want to:

  • Check that the length is less than 32 to start with
  • Check that every character is '0' or '1'

Just for LOLs, here's a LINQ version:

public static int ParseBinary(string input)
{
    return input.Select((c, index) => new { c, index })
        .Aggregate(0, (current, z) => current | (z.c - '0') << 
                                        (input.Length - z.index - 1));
}

Or neater yet:

public static int ParseBinary(string input)
{
    return return input.Aggregate(0, (current, c) => (current << 1) | (c - '0'));
}
花间憩 2024-10-12 01:26:52
string input = "10101001";
int output = 0;
for(int i = 7; i >= 0; i--)
{
  if(input[7-i] == '1')
    output += Math.Pow(2, i);
}

一般来说:

string input = "10101001";
int output = 0;
for(int i = (input.Length - 1); i >= 0; i--)
{
  if(input[input.Length - i] == '1')
    output += Math.Pow(2, i);
}
string input = "10101001";
int output = 0;
for(int i = 7; i >= 0; i--)
{
  if(input[7-i] == '1')
    output += Math.Pow(2, i);
}

in general:

string input = "10101001";
int output = 0;
for(int i = (input.Length - 1); i >= 0; i--)
{
  if(input[input.Length - i] == '1')
    output += Math.Pow(2, i);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文