从int获取单字节 [英] Get single byte from int

查看:200
本文介绍了从int获取单字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数.例如5630(十进制).二进制数字是:

I got an int number. For example 5630(decimal). The number in binary is:

00000000 00000000 00010101 11111110

00000000 00000000 00010101 11111110

我想获取十进制的第二个字节(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获取单字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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