# Recombining 2 bytes into a signed int

**URL:** <https://discourse.processing.org/t/recombining-2-bytes-into-a-signed-int/35067>\
**Category:** Coding Questions\
**Created:** [February 9, 2022, 11:14pm UTC](https://discourse.processing.org/t/recombining-2-bytes-into-a-signed-int/35067 "2022-02-09T23:14:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TKez](https://avatars.discourse-cdn.com/v4/letter/t/439d5e/32.png) [@TKez](https://discourse.processing.org/u/TKez)\
**Post date:** [February 9, 2022, 11:14pm UTC](https://discourse.processing.org/t/recombining-2-bytes-into-a-signed-int/35067/1 "2022-02-09T23:14:55Z")

</div>

Sending a byte stream over from an Arduino which includes some int16\_t values (little endian) but struggling to recombine them. This is as close as I can get, but I loose the sign.

```auto
          int1 = (inBuffer[1]<<8) | inBuffer[0]&0xff;
          int2 = (inBuffer[3]<<8) | inBuffer[2]&0xff;
          int3 = (inBuffer[5]<<8) | inBuffer[4]&0xff;
          int4 = (inBuffer[7]<<8) | inBuffer[6]&0xff;

```

Thanks.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 10, 2022, 12:10am UTC](https://discourse.processing.org/t/recombining-2-bytes-into-a-signed-int/35067/2 "2022-02-10T00:10:41Z")

</div>

[forum.Processing.org/two/discussion/17900/out-of-control-y-axis-rolling-graph#Item\_1](http://forum.processing.org/two/discussion/17900/out-of-control-y-axis-rolling-graph#Item_1)

---

<div class="post-metadata">

**Author:** ![TKez](https://avatars.discourse-cdn.com/v4/letter/t/439d5e/32.png) [@TKez](https://discourse.processing.org/u/TKez)\
**Post date:** [February 10, 2022, 12:35am UTC](https://discourse.processing.org/t/recombining-2-bytes-into-a-signed-int/35067/3 "2022-02-10T00:35:53Z")

</div>

Thanks but I can’t see an answer to my question there.

I need to use a byte stream for speed.

Sending ASCII and dealing with strings and tokens is way too slow for my requirements.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 10, 2022, 1:26am UTC](https://discourse.processing.org/t/recombining-2-bytes-into-a-signed-int/35067/4 "2022-02-10T01:26:20Z")

</div>

> [@TKez](#):
>
> … which includes some int16\_t values (little endian)…

Java’s corresponding datatype is `short`.

I’ve got this example sketch which uses a FloatBuffer:  
[forum.Processing.org/two/discussion/12757/transmission-of-a-float-through-a-serial-link#Item\_5](https://forum.processing.org/two/discussion/12757/transmission-of-a-float-through-a-serial-link#Item_5)

But you can easily adapt it to use a ShortBuffer instead:  
[docs.Oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html#asShortBuffer–](https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html#asShortBuffer--)

However I think you’re gonna need to switch your Arduino code to send bytes as big endian, which is the default protocol for transmission AFAIK.

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [February 10, 2022, 7:33am UTC](https://discourse.processing.org/t/recombining-2-bytes-into-a-signed-int/35067/5 "2022-02-10T07:33:19Z")

</div>

Hello,

Did you try:

```auto
int1 = ( (0xff & inBuffer[1]) << 8) | ( (0xff & inBuffer[0]) << 8)

```

I think that should work.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 16, 2022, 2:31pm UTC](https://discourse.processing.org/t/recombining-2-bytes-into-a-signed-int/35067/6 "2022-02-16T14:31:12Z")

</div>

> [@jb4x](#):
>
> ```auto
> int1 = ( (0xff & inBuffer[1]) << 8) | ( (0xff & inBuffer[0]) << 8)
> 
> ```
> 
> I think that should work.

It does not work.

Correction:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/eb7c14d4c72ea238c1b081592c0a132adf6e9c54.png)

`:)`

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [February 16, 2022, 2:45pm UTC](https://discourse.processing.org/t/recombining-2-bytes-into-a-signed-int/35067/7 "2022-02-16T14:45:02Z")

</div>

oops, thanks for the correction =)
