You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#中将二进制字符串解析为128位二进制字面量?

嘿,我明白你的需求了——就是要把普通整数转换成恰好128位的二进制字面量(比如那种bx开头、前面补满零的格式),同时还要确保用BigInteger存储的时候严格占16字节,对吧?毕竟常规类型存不下128位,BigInteger默认又会自动压缩字节数,得手动调整才行。下面是具体的实现思路和代码示例,用C#来演示(因为你提到了.NET的BigInteger类):

核心思路拆解
  • 生成128位二进制字面量:需要把整数的二进制表示补前导零(正数)或前导一(负数补码),确保总长度恰好128位,再加上bx前缀。
  • 构造16字节的BigInteger:默认BigInteger会用最少的字节存储数值(比如new BigInteger(1)只占1字节),所以要手动创建16字节的数组,把数值的字节按小端序复制进去,剩余位置补对应填充值(正数补0,负数补0xFF)。
具体实现代码

1. 生成128位二进制字面量的方法

using System.Numerics;

public static string Get128BitBinaryLiteral(BigInteger value)
{
    string binaryStr;

    if (value >= 0)
    {
        // 正数:直接转二进制,补前导零到128位
        binaryStr = value.ToString(2);
        int leadingZeros = 128 - binaryStr.Length;
        
        if (leadingZeros < 0)
            throw new ArgumentException("数值超出128位无符号整数范围(0 ~ 2^128-1)");
        
        binaryStr = leadingZeros > 0 ? new string('0', leadingZeros) + binaryStr : binaryStr;
    }
    else
    {
        // 负数:计算128位补码(取反加1)
        BigInteger absValue = BigInteger.Abs(value);
        string positiveBinary = absValue.ToString(2).PadLeft(128, '0');
        
        if (positiveBinary.Length > 128)
            throw new ArgumentException("数值超出128位有符号整数范围(-2^127 ~ 2^127-1)");
        
        // 取反
        char[] complementChars = positiveBinary.Select(c => c == '0' ? '1' : '0').ToArray();
        // 加1并补前导1到128位
        BigInteger complementVal = new BigInteger(Convert.ToInt64(new string(complementChars), 2)) + 1;
        binaryStr = complementVal.ToString(2).PadLeft(128, '1');
    }

    return $"bx{binaryStr}";
}

2. 构造16字节BigInteger的方法

public static BigInteger Get16ByteBigInteger(BigInteger value)
{
    byte[] originalBytes = value.ToByteArray();
    
    if (originalBytes.Length > 16)
        throw new ArgumentException("数值无法用16字节存储");

    // 创建16字节数组,小端序存储
    byte[] sixteenByteArr = new byte[16];
    
    if (value >= 0)
    {
        // 正数:剩余字节补0
        Array.Copy(originalBytes, sixteenByteArr, originalBytes.Length);
        Array.Fill(sixteenByteArr, (byte)0, originalBytes.Length, 16 - originalBytes.Length);
    }
    else
    {
        // 负数:剩余字节补0xFF(补码规则)
        Array.Copy(originalBytes, sixteenByteArr, originalBytes.Length);
        Array.Fill(sixteenByteArr, (byte)0xFF, originalBytes.Length, 16 - originalBytes.Length);
    }

    return new BigInteger(sixteenByteArr);
}
使用示例
// 测试整数1
BigInteger originalVal = new BigInteger(1);
string binaryLiteral = Get128BitBinaryLiteral(originalVal);
Console.WriteLine(binaryLiteral); 
// 输出:bx00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001

BigInteger sixteenByteVal = Get16ByteBigInteger(originalVal);
Console.WriteLine(sixteenByteVal.ToByteArray().Length); // 输出:16
注意事项
  • 范围限制:要区分有符号和无符号128位整数的范围,代码里已经做了检查,超出会抛出异常。
  • 字节序:BigInteger的字节数组是小端序的,所以复制的时候直接按原始顺序填充即可。
  • 字面量前缀:如果需要其他前缀(比如0b),直接修改Get128BitBinaryLiteral方法的返回字符串格式就行。

内容的提问来源于stack exchange,提问作者jburcham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:09:12