本文介绍了从int获取单字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个整数.例如5630(十进制).二进制数字是:
I got an int number. For example 5630(decimal). The number in binary is:
我想获取十进制的第二个字节(00010101).如何获得?
I want to get the second byte in decimal (00010101).How do I get it?
推荐答案
您可以使用 BitConverter.GetBytes()
:
You can use BitConverter.GetBytes()
:
int intValue = 5630;
byte[] intBytes = BitConverter.GetBytes(intValue);
byte result = intBytes[1]; // second least-significant byte
或仅向右移8位并转换为一个字节(截断左位):
or just bit-shift 8 places to the right and convert to a byte (truncating the left bits):
((byte)(intValue >> 8))
这篇关于从int获取单字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!