How do I concatenate 2 bytes?

前端 未结 6 1875
萌比男神i
萌比男神i 2021-02-08 02:18

I have 2 bytes:

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

How do I get 0x5a25 ?

6条回答
  •  没有蜡笔的小新
    2021-02-08 02:38

    The question is a little ambiguous.

    If a byte array you could simply: byte[] myarray = new byte[2]; myarray[0] = b1; myarray[1] = b2; and you could serialize the byearray...

    or if you're attempting to do something like stuffing these 16 bits into a int or similar you could learn your bitwise operators in c#... http://en.wikipedia.org/wiki/Bitwise_operation#Bit_shifts

    do something similar to:

    byte b1 = 0x5a; byte b2 = 0x25; int foo = ((int) b1 << 8) + (int) b2;

    now your int foo = 0x00005a25.

提交回复
热议问题