在C#中如何蚕食(半字节)字节? [英] How can you nibble (nybble) bytes in C#?

查看:210
本文介绍了在C#中如何蚕食(半字节)字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何让两个半从一个字节(高,低)使用C#以及如何组装两个半回一个字节。

I am looking to learn how to get two nibbles (high and low) from a byte using C# and how to assembly two nibbles back to a byte.

我使用C#和.NET 4.0,是否可以帮助有什么方法可以做,哪些库可用。

I am using C# and .Net 4.0 if that helps with what methods can be done and what libraries may be available.

推荐答案

您可以屏蔽掉4位字节的有四位,然后转向那些位字节中的最右边的位置:

You can 'mask off' 4 bits of a byte to have a nibble, then shift those bits to the rightmost position in the byte:

byte x = 0xA7;  // For example...
byte nibble1 = (byte) (x & 0x0F);
byte nibble2 = (byte)((x & 0xF0) >> 4);
// Or alternatively...
nibble2 = (byte)((x >> 4) & 0x0F);
byte original = (byte)((nibble2 << 4) | nibble1);

这篇关于在C#中如何蚕食(半字节)字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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