# Converting from byte to a HEX variable

**URL:** <https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651>\
**Category:** Programming\
**Created:** [March 21, 2023, 6:29am UTC](https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651 "2023-03-21T06:29:31Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![arduinodumbo](https://avatars.discourse-cdn.com/v4/letter/a/2acd7d/32.png) [@arduinodumbo](https://forum.arduino.cc/u/arduinodumbo)\
**Post date:** [March 21, 2023, 6:29am UTC](https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651/1 "2023-03-21T06:29:31Z")

</div>

Hi  
with my serial input, I am getting bytes and can print them as Serial.print(gotByte) or Serial.print(gotByte,HEX) or Serial.print(gotByte.HEX) and they print correctly.

BUT how do I convert the gotByte into a variables like gotHEX and gotDEC.  
I need to "look" for certain HEX codes and then join 2 bytes (i.e."FF74") and convert the result into a value including if it is a neg or pos (2s complement)  
Can Arduino do this ?  
Thanks  
Graham

---

<div class="post-metadata">

**Author:** ![paulpaulson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulpaulson/32/363643_2.png) [@paulpaulson](https://forum.arduino.cc/u/paulpaulson)\
**Post date:** [March 21, 2023, 6:31am UTC](https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651/2 "2023-03-21T06:31:18Z")

</div>

> [@arduinodumbo](#):
>
> Can Arduino do this ?

Yes.

Post your sketch, well formated, with well-tempered comments and in so called code tags "\</\>" to see how we can help.

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [March 21, 2023, 7:05am UTC](https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651/3 "2023-03-21T07:05:17Z")

</div>

Here's part of it, conversion

```cpp
// HEX text chars are '0' to '9' and 'A' to 'F'.

if ( char >= '0' && char <= '9' )
{
  val = char - '0';
}
else if ( char >= 'A' && char <= 'F' )
{
  val = char + 10 - 'A'; // text 'A' = hex A = decimal 10
}

```

There are 2 hex digits per byte, 4 bits each to assemble. Multiply the high val by 16 and add the low val to do that.

Text hex byte '37' --\> decimal 3 x 16 + 7 = 55

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [March 21, 2023, 8:12am UTC](https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651/4 "2023-03-21T08:12:47Z")

</div>

Assume that you have entered 1234 in the InputBox of Serial Monitor with Line ending option at Newline. Arduino UNO will receive them as 0x31 0x32 0x33 0x34 and newline character as 0x0A.

Now, tell us what you want to do with the first 4-byte? Do you want to save them in a variable as 1234 (BCD coded number) or as 04D2 (hex coded natural binary number)?

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [March 21, 2023, 8:21am UTC](https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651/5 "2023-03-21T08:21:52Z")

</div>

> [@GoForSmoke](#):
>
> `if ( char >= '0' && char <= '9' )`

_char_ is a keyword; can it be used as a variable?

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [March 21, 2023, 8:29am UTC](https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651/6 "2023-03-21T08:29:50Z")

</div>

> [@GolamMostafa](#):
>
> can it be used as a variable?

No.  
You define a variable with the data type char.  
char myVariable;

---

<div class="post-metadata">

**Author:** ![GolamMostafa](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/golammostafa/32/239011_2.png) [@GolamMostafa](https://forum.arduino.cc/u/GolamMostafa)\
**Post date:** [March 21, 2023, 8:38am UTC](https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651/7 "2023-03-21T08:38:05Z")

</div>

A variable is neither _initialized_ nor _assigned_ with a value; does Compiler allocate memory space for it?

---

<div class="post-metadata">

**Author:** ![theeccentricgenius](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/theeccentricgenius/32/993275_2.png) [@theeccentricgenius](https://forum.arduino.cc/u/theeccentricgenius)\
**Post date:** [March 21, 2023, 11:20am UTC](https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651/8 "2023-03-21T11:20:30Z")

</div>

Can you use c/c++ strtol(), and set it to base 16?

---

<div class="post-metadata">

**Author:** ![Perehama](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perehama/32/256713_2.png) [@Perehama](https://forum.arduino.cc/u/Perehama)\
**Post date:** [March 21, 2023, 11:56am UTC](https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651/9 "2023-03-21T11:56:22Z")

</div>

Since the conversion works as expected when using Serial.print(), you can borrow from the [source code](https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/Print.cpp) since it is freely accessible.

---

<div class="post-metadata">

**Author:** ![PaulRB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paulrb/32/216330_2.png) [@PaulRB](https://forum.arduino.cc/u/PaulRB)\
**Post date:** [March 21, 2023, 12:38pm UTC](https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651/10 "2023-03-21T12:38:31Z")

</div>

> [@arduinodumbo](#):
>
> Serial.print(gotByte,HEX) or Serial.print(gotByte.HEX)

That first function will print the byte as hex characters, no problem. I don't think that second one will even compile. What did you mean, exactly?

> [@arduinodumbo](#):
>
> BUT how do I convert the gotByte into a variables like gotHEX and gotDEC.

Why do you want to do that, exactly? How would the variable gotByte be different to gotHEX? Remember, all variables are stored in the Arduino's memory in binary.

> [@arduinodumbo](#):
>
> I need to "look" for certain HEX codes

If you want to check that gotByte is a certain hex value such as C9, use

```arduino
if (gotByte == 0xC9)

```

> [@arduinodumbo](#):
>
> join 2 bytes (i.e."FF74") and convert the result into a value

Use the standard arduino function:

```arduino
int value = word(gotByteHigh, gotByteLow);

```

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [March 23, 2023, 6:39am UTC](https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651/11 "2023-03-23T06:39:09Z")

</div>

When writing pseudocode... YES.

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [March 23, 2023, 6:41am UTC](https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651/12 "2023-03-23T06:41:55Z")

</div>

one of the keys to communication is figuring out where you missed not just the point but the whole pencil and never needed to take a trip to absurdity.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 19, 2023, 6:42am UTC](https://forum.arduino.cc/t/converting-from-byte-to-a-hex-variable/1104651/13 "2023-09-19T06:42:29Z")

</div>

Closed on September 19, 2023, 6:42am UTC

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