# Insert an integer and convert into HEX array

Hi I have a HEX array like this:

uint8_t my_serial_bytes={0x55, 0x01, 0x00};

Now I want to create a function to update a HEX in this array. For example:

void changeVal(int val){
my_serial_bytes[1]=val;
}

changeVal(30); //my_serial_bytes[0x55, 0x1E, 0x00];

So if I call function “changeVal(30);”, The array will become my_serial_bytes[0x55, 0x1E, 0x00];

How do I do this? Thanks a lot for any inputs.

What's wrong with the approach you've outlined?

Keep in mind that HEX is just a human readable way of representing a binary number, which is how it is stored.

Here is my script:

char incomingByte = 0;
uint8_t my_serial_bytes={0x55, 0x01, 0xF0};

void setup() {
Serial.begin(9600);
}

void loop() {
if (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.println(my_serial_bytes[1]);
changeVal(incomingByte);
Serial.println(my_serial_bytes[1]);
}
}
void changeVal(int val){
my_serial_bytes[1]=val;
}

If I type in 1, it comes out
1
49
not
1
1
Seems like I need to convert to integer?

Quote tags are not code tags!

That’s because you use serial/Serial monitor. You don’t input 1, you input ‘1’, the ASCII char for a 1 character. And that has the value of 49 in decimal. If you only want numbers between 0 and 9 it’s an easy fix.

``````uint8_t my_serial_bytes[]={0x55, 0x01, 0xF0};

void setup() {
Serial.begin(9600);
}

void loop() {
if (Serial.available() > 0) {
Serial.println(my_serial_bytes[1]);
changeVal(Serial.read() - '0');
Serial.println(my_serial_bytes[1]);
}
}
void changeVal(uint8_t val){
my_serial_bytes[1]=val;
}
``````

If you want bigger numbers to input via Serial it’s more difficult.

A "1" on your keyboard is an ASCII "49". That is how characters are stored internally on a computer. You could not only solve your problem, but learn a bunch by reading up on ASCII. Serial.print() is an overloaded function, it adapts itself to the datatype that you pass to it. You passed it a uint8_t, so it printed the value as an unsigned 8 bit integer, not an ASCII character.

The atoi() function converts from numbers like ASCII "1" to the numerical value 1. Or "123" to 123.

There is no such thing as a "hex array". Not in C. Hex is a human readable format for binary data. You can type, read and think in hex, but the computer stores everything in pure binary.

Numbers are not hex or decimal. Nether are they binary, roman numerals, or Sumerian cuneiform. Numbers are numbers. Hex is simply the way we write them down.