如何串联2个字节? [英] How do I concatenate 2 bytes?

查看:126
本文介绍了如何串联2个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个字节:

byte b1 = 0x5a;  
byte b2 = 0x25;

如何获取 0x5a25

推荐答案

可以使用按位运算符'<<'和'|'

It can be done using bitwise operators '<<' and '|'

public int Combine(byte b1, byte b2)
{
    int combined = b1 << 8 | b2;
    return combined;
}

用法示例:

[Test]
public void Test()
{
    byte b1 = 0x5a;
    byte b2 = 0x25;
    var combine = Combine(b1, b2);
    Assert.That(combine, Is.EqualTo(0x5a25));
}

这篇关于如何串联2个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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