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.
I am using C# and .Net 4.0 if that helps with what methods can be done and what libraries may be available.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With