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

在Unity(.NET 3.5)中将大十进制整数转换为字节数组的方法

大十进制整数转字节数组(.NET 3.5 Unity环境)

在Unity的.NET 3.5环境里没法用BigInteger确实挺头疼的,不过我们可以根据你的大整数的具体情况,用两种不同的方案来解决十进制转字节数组的问题:

情况1:整数在Decimal类型范围内(无小数部分)

如果你的大整数还没超出Decimal类型的上限(Decimal能表示的最大整数是79228162514264337593543950335),而且是纯整数(没有小数部分),那我们可以直接利用Decimal的内部结构来转换:

Decimal在.NET里本质是由四个32位整数组成的:三个存储有效数字,一个存储缩放因子和符号。我们可以提取这些整数,再转成字节数组。

实现代码

using System;
using System.Collections.Generic;

public static class DecimalConverter
{
    public static byte[] DecimalIntegerToByteArray(decimal value)
    {
        // 先验证输入是纯整数
        if (value != Math.Floor(value))
            throw new ArgumentException("输入必须是整数类型的Decimal", nameof(value));
        
        // 获取Decimal内部的四个int组件
        int[] decimalBits = Decimal.GetBits(value);
        
        // 将每个int转换为字节并拼接(默认小端字节序,和BitConverter一致)
        List<byte> byteList = new List<byte>();
        foreach (int bit in decimalBits)
        {
            byteList.AddRange(BitConverter.GetBytes(bit));
        }
        
        // 负数的符号已经包含在第四个int的最高位里,无需额外处理
        return byteList.ToArray();
    }
}

使用说明

  • 调用时直接传入纯整数的decimal值即可,比如DecimalConverter.DecimalIntegerToByteArray(1234567890123456789m)
  • 得到的字节数组包含了原Decimal的完整二进制信息,包括符号位

情况2:整数超出Decimal范围(字符串形式的超大整数)

如果你的数大到连Decimal都存不下(比如超过前面说的最大整数),那只能从字符串入手,手动实现十进制到二进制的转换,再把二进制位组合成字节数组。

实现代码

using System;
using System.Collections.Generic;

public static class LargeDecimalConverter
{
    public static byte[] LargeDecimalStringToByteArray(string decimalStr)
    {
        // 验证输入合法性
        if (string.IsNullOrEmpty(decimalStr))
            throw new ArgumentNullException(nameof(decimalStr));
        if (!decimalStr.All(c => char.IsDigit(c) || c == '-'))
            throw new ArgumentException("输入必须是有效的十进制数字字符串", nameof(decimalStr));
        
        // 处理负数标记
        bool isNegative = false;
        if (decimalStr.StartsWith("-"))
        {
            isNegative = true;
            decimalStr = decimalStr.Substring(1);
        }
        
        List<char> digits = decimalStr.ToList();
        List<byte> binaryBits = new List<byte>();
        
        // 逐次除以2,收集余数(余数就是二进制位,从低位到高位)
        while (digits.Count > 0)
        {
            int remainder = 0;
            List<char> newDigits = new List<char>();
            
            foreach (char c in digits)
            {
                int currentNum = remainder * 10 + (c - '0');
                int quotient = currentNum / 2;
                remainder = currentNum % 2;
                
                // 跳过前导零,除非商本身就是0
                if (newDigits.Count > 0 || quotient != 0)
                {
                    newDigits.Add((char)('0' + quotient));
                }
            }
            
            binaryBits.Add((byte)remainder);
            digits = newDigits;
        }
        
        // 将二进制位凑成8位一组,转换为字节(小端模式)
        while (binaryBits.Count % 8 != 0)
        {
            binaryBits.Add(0);
        }
        
        List<byte> byteArray = new List<byte>();
        for (int i = 0; i < binaryBits.Count; i += 8)
        {
            byte currentByte = 0;
            for (int j = 0; j < 8; j++)
            {
                currentByte |= (byte)(binaryBits[i + j] << j);
            }
            byteArray.Add(currentByte);
        }
        
        // 处理负数:转换为补码形式
        if (isNegative)
        {
            // 第一步:按位取反
            for (int i = 0; i < byteArray.Count; i++)
            {
                byteArray[i] = (byte)~byteArray[i];
            }
            
            // 第二步:加1
            int carry = 1;
            for (int i = 0; i < byteArray.Count; i++)
            {
                int sum = byteArray[i] + carry;
                byteArray[i] = (byte)(sum & 0xFF);
                carry = sum >> 8;
                if (carry == 0)
                    break;
            }
            
            // 如果还有进位,补一个0xFF(保持有符号数的补码规则)
            if (carry != 0)
            {
                byteArray.Add(0xFF);
            }
        }
        
        return byteArray.ToArray();
    }
}

使用说明

  • 直接传入超大整数的字符串形式,比如LargeDecimalConverter.LargeDecimalStringToByteArray("1234567890123456789012345678901234567890")
  • 负数直接在字符串前加-即可,方法会自动处理补码转换

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:02:45