Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/NGrib/BadGribFormatException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,20 @@
* along with NGrib. If not, see <https://www.gnu.org/licenses/>.
*/

using System;
namespace NGrib;

namespace NGrib
/// <summary>
/// The exception that is thrown when the read stream does not
/// respect the GRIB 2 data format.
/// </summary>
public class BadGribFormatException : Exception
{
/// <summary>
/// The exception that is thrown when the read stream does not
/// respect the GRIB 2 data format.
/// </summary>
public class BadGribFormatException : Exception
{
/// <summary>
/// Initializes a new instance of the <see cref="BadGribFormatException"/> class
/// with a specified error message.
/// </summary>
/// <param name="message">The message that describes the error.</param>
public BadGribFormatException(string message) : base(message)
{
}
}
{
}
}
124 changes: 61 additions & 63 deletions src/NGrib/BigEndianBitConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,77 +24,75 @@
* along with NGrib. If not, see <https://www.gnu.org/licenses/>.
*/

using System;
using System.Numerics;

namespace NGrib
namespace NGrib;

internal static class BigEndianBitConverter
{
internal static class BigEndianBitConverter
{
public const int Int8MinValue = -127;
public const int Int32MinValue = -2147483647;
public const int Int8MinValue = -127;
public const int Int32MinValue = -2147483647;

public static int ToInt8(byte[] data, int startIndex) => ToInt8(data[startIndex]);
public static int ToInt8(byte[] data, int startIndex) => ToInt8(data[startIndex]);

public static int ToInt8(byte data) => (1 - ((data & 128) >> 6)) * (data & 127);
public static int ToInt8(byte data) => (1 - ((data & 128) >> 6)) * (data & 127);

public static int ToUInt16(byte[] data, int startIndex)
{
var index = startIndex;
return (data[index++] << 8) | data[index];
}
public static int ToUInt16(byte[] data, int startIndex)
{
var index = startIndex;
return (data[index++] << 8) | data[index];
}

public static int ToInt16(byte[] data, int startIndex)
{
var index = startIndex;
return (1 - ((data[index] & 128) >> 6)) * ((data[index++] & 127) << 8 | data[index]);
}
public static int ToInt16(byte[] data, int startIndex)
{
var index = startIndex;
return (1 - ((data[index] & 128) >> 6)) * ((data[index++] & 127) << 8 | data[index]);
}

public static long ToUInt32(byte[] data, int startIndex)
{
var index = startIndex;
return ((uint) data[index++] << 24) |
((uint) data[index++] << 16) |
((uint) data[index++] << 8) |
data[index];
}
public static long ToUInt32(byte[] data, int startIndex)
{
var index = startIndex;
return ((uint) data[index++] << 24) |
((uint) data[index++] << 16) |
((uint) data[index++] << 8) |
data[index];
}

public static BigInteger ToUInt64(byte[] data, int startIndex)
{
var index = startIndex;
return ((ulong) data[index++] << 56) |
((ulong) data[index++] << 48) |
((ulong) data[index++] << 40) |
((ulong) data[index++] << 32) |
((ulong) data[index++] << 24) |
((ulong) data[index++] << 16) |
((ulong) data[index++] << 8) |
data[index];
}
public static BigInteger ToUInt64(byte[] data, int startIndex)
{
var index = startIndex;
return ((ulong) data[index++] << 56) |
((ulong) data[index++] << 48) |
((ulong) data[index++] << 40) |
((ulong) data[index++] << 32) |
((ulong) data[index++] << 24) |
((ulong) data[index++] << 16) |
((ulong) data[index++] << 8) |
data[index];
}

public static int ToInt32(byte[] data, int startIndex)
{
var index = startIndex;
return (1 - ((data[index] & 128) >> 6)) * ((data[index++] & 127) << 24 |
data[index++] << 16 |
data[index++] << 8 |
data[index]);
}
public static int ToInt32(byte[] data, int startIndex)
{
var index = startIndex;
return (1 - ((data[index] & 128) >> 6)) * ((data[index++] & 127) << 24 |
data[index++] << 16 |
data[index++] << 8 |
data[index]);
}

public static float ToSingle(byte[] data, int startIndex)
{
if (BitConverter.IsLittleEndian)
{
var reversedBytes = new[]
{
data[startIndex+3],
data[startIndex+2],
data[startIndex+1],
data[startIndex]
};
return BitConverter.ToSingle(reversedBytes, 0);
}
return BitConverter.ToSingle(data, startIndex);
}
}
}
public static float ToSingle(byte[] data, int startIndex)
{
if (BitConverter.IsLittleEndian)
{
var reversedBytes = new[]
{
data[startIndex+3],
data[startIndex+2],
data[startIndex+1],
data[startIndex]
};
return BitConverter.ToSingle(reversedBytes, 0);
}
return BitConverter.ToSingle(data, startIndex);
}
}
Loading