# Limit a hexadecimal number to 8-bits

**URL:** <https://discourse.nodered.org/t/limit-a-hexadecimal-number-to-8-bits/94795>\
**Category:** General\
**Created:** [21 January 2025 17:53 UTC](https://discourse.nodered.org/t/limit-a-hexadecimal-number-to-8-bits/94795 "2025-01-21T17:53:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![redhead](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@redhead](https://discourse.nodered.org/u/redhead)\
**Post date:** [21 January 2025 17:53 UTC](https://discourse.nodered.org/t/limit-a-hexadecimal-number-to-8-bits/94795/1 "2025-01-21T17:53:35Z")

</div>

I'm working on a serial interface. The serial packet includes the checksum. The checksum is limited to 8-bits (two-digit hexadecimal number). Any 8-bit math overflows are ignored.  
For example, if the checksum is hexadecimal 1AF the serial packet only sends hexadecimal AF as the checksum.

**I have written the code to get the checksum, but I have not been able to figure out how to limit the checksum to a two-digit hexadecimal number.**

Here is the statement from the protocol document: The Checksum field is an unsigned 8-bit value that is calculated as the sum of the Length field, the Type field, and all the data bytes. During the summation of the fields, any 8-bit math overflows are ignored, i.e. the result is the sum modulo 256.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [21 January 2025 18:21 UTC](https://discourse.nodered.org/t/limit-a-hexadecimal-number-to-8-bits/94795/2 "2025-01-21T18:21:11Z")

</div>

Use the Bitwise AND (`&`) operator

e.g...

```auto
const checksum = this + that + other
const checksumDecimal = checksum & 0xff // use checksumDecimal if populating a buffer
const checksumHex = checksumDecimal .toString(16) // use checksumHex is appending to a string

```

e.g...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/1/21070d04c997efa178f8a56f16732564ab393368.png)

---

<div class="post-metadata">

**Author:** ![redhead](https://avatars.discourse-cdn.com/v4/letter/r/9e8a1a/32.png) [@redhead](https://discourse.nodered.org/u/redhead)\
**Post date:** [21 January 2025 18:35 UTC](https://discourse.nodered.org/t/limit-a-hexadecimal-number-to-8-bits/94795/3 "2025-01-21T18:35:31Z")

</div>

Perfect! Thank you!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 February 2025 18:35 UTC](https://discourse.nodered.org/t/limit-a-hexadecimal-number-to-8-bits/94795/4 "2025-02-04T18:35:59Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
