I got an int number. For example 5630(decimal). The number in binary is:
00000000 00000000 00010101 11111110
I want to get the second byte in decimal (00010101). How do I get it?
You can use BitConverter.GetBytes()
:
int intValue = 5630;
byte[] intBytes = BitConverter.GetBytes(intValue);
byte result = intBytes[1]; // second least-significant byte
or just bit-shift 8 places to the right and convert to a byte (truncating the left bits):
((byte)(intValue >> 8))
do a bitwise And with 00000000 00000000 11111111 00000000
(in hex, 0xFF00
), and right shift by 8 places.
var x = 5630;
var secondByte = (x & 0xFF00) >> 8;
or, bit-shift first and strip off higher order bytes by &-ing with value
00000000 00000000 00000000 11111111
decimal 255, or (hex 0xFF
)
var x = 5630;
var secondByte = (x >> 8) & 0xFF;
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