【问题标题】:How do I convert byte values into decimals?如何将字节值转换为小数?
【发布时间】:2010-08-12 13:00:50
【问题描述】:

我正在尝试从文件中加载一些十进制值,但我无法找到正确的方法来获取原始值并将它们转换为小数。

我已将文件读入一个字节数组,每个四个字节的块应该代表一个十进制值。为了帮助解决这个问题,我构建了一个表格,说明从 1 到 46 的十进制值如何表示为四个字节块。

例如,数字 1 显示为 0,0,128,63,数字 2 显示为 0,0,0,64,依此类推,直到 46,即 0,0,56,66。完整表格可在here 获取。

还有另一个数字系列,保留三位小数并包括负数,即here。

我拥有的唯一文档说明

它们首先存储最低有效字节:1、256、65536、16777216。这使得十六进制序列 01 01 00 00 变为数字 257(十进制)。在 C/C++ 中,阅读例如一个浮动,做:浮动x; fread(&x, sizeof(float), 1, fileptr);

但是,我使用的是 .NET 的 File.ReadAllBytes 方法,所以这并没有多大帮助。如果有人能抽出几分钟时间查看示例文件,看看他们是否能找到将值转换为小数的方法,我将不胜感激。

【问题讨论】:

    标签: c# .net


    【解决方案1】:

    您可以使用BitConverter.ToSingle 从字节数组中读取浮点值,因此要获取浮点序列,您可以执行以下操作:

    byte[] data = File.ReadAllBytes(fileName);
    int count = data.Length / 4;
    Debug.Assert(data.Length % 4 == 0);
    
    IEnumerable<float> values = Enumerable.Range(0, count)
        .Select(i => BitConverter.ToSingle(data, i*4));
    

    【讨论】:

    • 我已将其标记为正确,因为它稍微详细一些。非常感谢,没想到这么简单。
    【解决方案2】:

    您是否考虑过使用 BitConverter 类?它在字节数组和各种类型之间进行转换。

    编辑: MSDN 在http://msdn.microsoft.com/en-us/library/system.bitconverter_methods(v=vs.85).aspx 对 BitConverter 的文档有一个有用的评论:

    public static decimal ToDecimal(byte[] bytes)
    {
      int[] bits = new int[4];
      bits[0] = ((bytes[0] | (bytes[1] << 8)) | (bytes[2] << 0x10)) | (bytes[3] << 0x18); //lo
      bits[1] = ((bytes[4] | (bytes[5] << 8)) | (bytes[6] << 0x10)) | (bytes[7] << 0x18); //mid
      bits[2] = ((bytes[8] | (bytes[9] << 8)) | (bytes[10] << 0x10)) | (bytes[11] << 0x18); //hi
      bits[3] = ((bytes[12] | (bytes[13] << 8)) | (bytes[14] << 0x10)) | (bytes[15] << 0x18); //flags
    
      return new decimal(bits);
    }
    
    public static byte[] GetBytes(decimal d)
    {
      byte[] bytes = new byte[16];
    
      int[] bits = decimal.GetBits(d);
      int lo = bits[0];
      int mid = bits[1];
      int hi = bits[2];
      int flags = bits[3];
    
      bytes[0] = (byte)lo;
      bytes[1] = (byte)(lo >> 8);
      bytes[2] = (byte)(lo >> 0x10);
      bytes[3] = (byte)(lo >> 0x18);
      bytes[4] = (byte)mid;
      bytes[5] = (byte)(mid >> 8);
      bytes[6] = (byte)(mid >> 0x10);
      bytes[7] = (byte)(mid >> 0x18);
      bytes[8] = (byte)hi;
      bytes[9] = (byte)(hi >> 8);
      bytes[10] = (byte)(hi >> 0x10);
      bytes[11] = (byte)(hi >> 0x18);
      bytes[12] = (byte)flags;
      bytes[13] = (byte)(flags >> 8);
      bytes[14] = (byte)(flags >> 0x10);
      bytes[15] = (byte)(flags >> 0x18);
    
      return bytes;
    }
    

    【讨论】:

    • 谢谢,这绝对是要走的路。
    • 很遗憾,它没有实现十进制转换。
    • @EranBetzalel MSDN 有一些代码来实现 BitConverter 的“缺失”成员。查看我编辑的更新。
    • @BlueMonkMN,这不是 MSDN 的,只是某个人把它贴在那里,他可能是从我从同一个地方拿的 - .Net decomplided 库。它与 BitConvertor 无关。
    • @EranBetzalel 我的意思是,包含内容的是 MSDN 的站点和可以找到该内容的 BitConverter 的页面。我并没有声称它与 MSDN 或 bitconverter 有内在联系(我什至注意到这是一个“评论”)。但是,无论是谁编写或在哪里编写的,解决方案都是解决方案。现在解决方案在 StackOverflow 上。这取决于你,编码员,看看代码是否适合你,而不是根据它来自哪里的凭据来确定它。代码可以根据自己的优点独立判断。
    【解决方案3】:

    .NET 库在内部实现了Decimal.GetBytes() 方法。

    我使用反编译的 .NET 库创建了十进制和字节数组之间的简单转换方法 - 你可以在这里找到它:

    https://gist.github.com/eranbetzalel/5384006#file-decimalbytesconvertor-cs

    编辑:这是来自我的链接的完整源代码。

    public decimal BytesToDecimal(byte[] buffer, int offset = 0)
    {
      var decimalBits = new int[4];
    
      decimalBits[0] = buffer[offset + 0] | (buffer[offset + 1] << 8) | (buffer[offset + 2] << 16) | (buffer[offset + 3] << 24);
      decimalBits[1] = buffer[offset + 4] | (buffer[offset + 5] << 8) | (buffer[offset + 6] << 16) | (buffer[offset + 7] << 24);
      decimalBits[2] = buffer[offset + 8] | (buffer[offset + 9] << 8) | (buffer[offset + 10] << 16) | (buffer[offset + 11] << 24);
      decimalBits[3] = buffer[offset + 12] | (buffer[offset + 13] << 8) | (buffer[offset + 14] << 16) | (buffer[offset + 15] << 24);
    
      return new Decimal(decimalBits);
    }
    
    public byte[] DecimalToBytes(decimal number)
    {
      var decimalBuffer = new byte[16];
    
      var decimalBits = Decimal.GetBits(number);
    
      var lo = decimalBits.Value[0];
      var mid = decimalBits.Value[1];
      var hi = decimalBits.Value[2];
      var flags = decimalBits.Value[3];
    
      decimalBuffer[0] = (byte)lo;
      decimalBuffer[1] = (byte)(lo >> 8);
      decimalBuffer[2] = (byte)(lo >> 16);
      decimalBuffer[3] = (byte)(lo >> 24);
    
      decimalBuffer[4] = (byte)mid;
      decimalBuffer[5] = (byte)(mid >> 8);
      decimalBuffer[6] = (byte)(mid >> 16);
      decimalBuffer[7] = (byte)(mid >> 24);
    
      decimalBuffer[8] = (byte)hi;
      decimalBuffer[9] = (byte)(hi >> 8);
      decimalBuffer[10] = (byte)(hi >> 16);
      decimalBuffer[11] = (byte)(hi >> 24);
    
      decimalBuffer[12] = (byte)flags;
      decimalBuffer[13] = (byte)(flags >> 8);
      decimalBuffer[14] = (byte)(flags >> 16);
      decimalBuffer[15] = (byte)(flags >> 24);
    
      return decimalBuffer;
    }
    

    【讨论】:

      【解决方案4】:

      正如其他人所提到的,使用BitConverter 类,请参见下面的示例:

           byte[] bytez = new byte[] { 0x00, 0x00, 0x80, 0x3F };
           float flt = BitConverter.ToSingle(bytez, 0); // 1.0
      
           bytez = new byte[] { 0x00, 0x00, 0x00, 0x40 };
           flt = BitConverter.ToSingle(bytez, 0); // 2.0
      
           bytez = new byte[] { 0, 0, 192, 190 };
           flt = BitConverter.ToSingle(bytez, 0); // -0.375
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-20
        • 2010-11-04
        • 2010-12-24
        • 2013-01-27
        • 1970-01-01
        • 1970-01-01
        • 2018-01-18
        相关资源
        最近更新 更多