有没有在C#中的INT24实现? [英] Are there any Int24 implementations in C#?

查看:1284
本文介绍了有没有在C#中的INT24实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的,我要求并不在.NET基础类库中存在的数据类型的一个项目 - 一个无符号INT24。 。对于我在做INT32,第4个字节计算,即使全部设置为零,砸了我的结果。

I have a project that I am working on where I require a data type that doesn't exist in the .NET BCL - an Unsigned Int24. For calculations that I am doing, the 4th byte in an int32, even when set to all zeroes, screws up my results.

编辑:
我做在一个24位整数空间仅限于24位数据逐位循环移位。如果旋转在24位一级的32位数字进行的结果是疯狂不正确。

I'm doing bitwise circular shifts on a 24bit integer space that is limited to only 24bits. If the rotation is performed at 24bit level on a 32bit number the result is wildly incorrect.

有谁知道可用此数据类型的任何第三方的实现?

Does anyone know of any third party implementations of this data type that are available?

谢谢!

推荐答案

实施INT24并不难(说实话!)。但是,我们需要更多地了解为什么你需要实现它。 @nneonneo奇迹,如果你想与使用24位整数的本地库接口。

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); 
        _b2 = (byte)(value >> 16);
    }

    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 );



修改为大端或签署INT24类是一个练习留给了读者。

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

这篇关于有没有在C#中的INT24实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆