BitConverter.ToInt32 的替代方案

发布于 2024-08-27 06:10:05 字数 503 浏览 4 评论 0原文

我正在使用 BitConverter.ToInt32 将 3 个 byte 值打包到 int 中,如下所示:

byte R = 0;
byte G = 0;
byte B = 0;
int i = BitConverter.ToInt32(new byte[] { R, G, B, 0 }, 0);

是否有更快的方法来执行此操作不需要每次都创建一个新的int吗?从 int 中获取字节很容易:

int i = 34234;
byte B = (byte)(i >> 0);
byte G = (byte)(i >> 8);
byte R = (byte)(i >> 16);

是否有一种简单的方法来反转此过程并使用位移位将 RGB 字节写回到现有的 int 上?

I'm using BitConverter.ToInt32 to pack 3 byte values into an int, like so:

byte R = 0;
byte G = 0;
byte B = 0;
int i = BitConverter.ToInt32(new byte[] { R, G, B, 0 }, 0);

Is there a faster way to do this that doesn't involve the creation of a new int each time? Getting the bytes out of an int is easy:

int i = 34234;
byte B = (byte)(i >> 0);
byte G = (byte)(i >> 8);
byte R = (byte)(i >> 16);

Is there a simple way to reverse this process and use bit-shifting to write the RGB bytes back over an existing int?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

韵柒 2024-09-03 06:10:05
int i = (B << 0) | (G << 8) | (R << 16);
int i = (B << 0) | (G << 8) | (R << 16);
坐在坟头思考人生 2024-09-03 06:10:05

您应该考虑颜色结构。它具有 R、G 和 B 属性以及 FromArgb() 和 ToArgb() 方法。

You ought to consider the Color structure. It has R, G and B properties and FromArgb() and ToArgb() methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文