4

.NET BCL に存在しないデータ型 (Unsigned Int24) が必要なプロジェクトに取り組んでいます。私が行っている計算では、int32 の 4 番目のバイトは、すべてゼロに設定されている場合でも、結果を台無しにします。

編集: 24 ビットのみに制限されている 24 ビット整数空間でビット単位の循環シフトを行っています。ローテーションが 32 ビットの数値で 24 ビット レベルで実行されると、結果は非常に不正確になります。

利用可能なこのデータ型のサードパーティの実装を知っている人はいますか?

ありがとう!

4

1 に答える 1

8

Implementing Int24 isn't hard (honest!). But we need to know more about why you need to implement it. @nneonneo wonders if you're trying to interface with a native library that uses 24-bit integers. If that's the case then you can be done by doing something like this:

[StructLayout(LayoutKind.Sequential)]
public struct UInt24 {
    private Byte _b0;
    private Byte _b1;
    private Byte _b2;

    public UInt24(UInt32 value) {
        _b0 = (byte)( (value      ) & 0xFF );
        _b1 = (byte)( (value >>  8) & 0xFF ); 
        _b2 = (byte)( (value >> 16) & 0xFF );
    }

    public unsafe Byte* Byte0 { get { return &_b0; } }

    public UInt32 Value { get { return _b0 | ( _b1 << 8 ) | ( _b2 << 16 ); } }
}

// Usage:

[DllImport("foo.dll")]
public static unsafe void SomeImportedFunction(byte* uint24Value);

UInt24 uint24 = new UInt24( 123 );
SomeImportedFunction( uint24.Byte0 );

Modifying the class for big-endian or signed Int24 is an exercise left up to the reader.

于 2012-09-23T02:26:13.553 に答える