Copy int array to byte

Hi everyone.

I am a beginner and i am a bit stuck right now.

I am receiving via serial monitor data like 1234567812345678 several times. I save them everytime in a int variable: addr[100][16]. To be able to receive data 100 times maximum.
Once i save the data in the ‘addr’ variable, I would like to copy it into a byte variable: byte idx.

int addr[100][16]; //save receiving data. 1234567812345678 e.g.
byte idx;

for (int i = 0; i < 15; i++) { //store the incoming data from an external device
             addr[counter][i]=response.getValue()[i];
             Serial.print(addr[counter][i]);
           }

for (int i=0; i<counter;i++){
   idx=addr[i];
}

As you see, I only want to store the incoming data and be able to copy the 16 ‘addr’ numbers into the ‘idx’ byte.

I don´t really know how to do it after searching on internet. I may be programming in the wrong way.
I would aprpeciate any suggestion.
Thank you.

I save them everytime in a int variable:

Why an int variable ?

I only want to store the incoming data and be able to copy the 16 'addr' numbers into the 'idx' byte

How are 16 numbers of any kind going to fit in a byte ?

AntonioGR:
I am receiving via serial monitor data like 1234567812345678 several times. I save them everytime in a int variable: addr[100][16]. To be able to receive data 100 times maximum.

It is not clear at all to me what you want to do. Is the data from the serial monitor ASCII data (you are typing it in)? If so, are you wanting to convert it to an integer or integers? What are you going to do with the data?

This array alone will consume 3200 bytes. int addr[100][16];. If you are using an UNO you will exceed the amount of RAM available.

If you want some help it might be beneficial for us to know what you are trying to accomplish.

UKHeliBob:
Why an int variable ?
How are 16 numbers of any kind going to fit in a byte ?

Im sorry I did not explain well myself. The 16 incoming bits are stored in two different array variables:
-uint32_t addr1[8] stores the first 8 numbers.
-uint32_t addr2 [8]stores the last 8 numbers.

Once i got it. It is possible to copy the 8 numbers from the addr1 and addr2 to a byte variable, each one.

ToddL1962:
It is not clear at all to me what you want to do. Is the data from the serial monitor ASCII data (you are typing it in)? If so, are you wanting to convert it to an integer or integers? What are you going to do with the data?

This array alone will consume 3200 bytes. int addr[100][16];. If you are using an UNO you will exceed the amount of RAM available.

If you want some help it might be beneficial for us to know what you are trying to accomplish.

The data from serial monitor are the 16 bit address of an XBee, the High and Low number address. I use 'addr' to save these 16 numbers: 0013a20040xxxxxx. Then I separate the first 8 numbers and the last 8 numbers in two:
--> uint32_t addr1[8] and uint32_t addr2[8].
But I would like to know wether is possible to separate them using a byte for each one: byte for 0013a200, and another byte for the rest.
Thank you for your time.
And forgive If I dont explain myself well sometimes, Im spanish. '_'

Was this example 1234567812345678 using the digits 1-8 as placeholders for binary digit characters, either ‘0’ or ‘1’ in each place? That’s the only way this will work. You obviously cannot store any of the digits ‘2’ through ‘8’ in a single bit!

Why bother with the extremely wasteful uint32_t addr1[8] and uint32_t addr2[8] in the first place?

Just read the bits into a 16 bit “shift register” variable as the come in…

uint16_t value = 0;
for (uint8_t i = 0; i < 16; i++)
{
   value <<= 1;
   if (next_received_bit == 1) value |= 1;
}
uint8_t byte1 = value >> 8;
uint8_t byte2 = value & 0xFF;

pcbbc:
Was this example 1234567812345678 using the digits 1-8 as placeholders for binary digit characters, either '0' or '1' in each place?

Not really. The real data I get is 0013a20040xxxxxx. A total of 16 bits. The first part is 0013a200 and the second one 40xxxxxx. That two parts are which I want to save in a byte each one. It may not be possible to do, but I dont really know. That's what I'm looking for, if you know how to do it or another better way.
Thank you.

Here you can see how I break down a 32 bit int,*item, into 4 bytes:

 if (!PassTwo)
        {
          rx_frame.FIR.B.FF = CAN_frame_std;
          rx_frame.MsgID = 1;
          rx_frame.FIR.B.DLC = 8;
          rx_frame.data.u8[0] = *item & 0xFF;
          rx_frame.data.u8[1] = (*item >> 8) & 0xFF;
          rx_frame.data.u8[2] = (*item >> 16) & 0xFF;
          rx_frame.data.u8[3] = (*item >> 24) & 0xFF;
          PassTwo = true;
        } else {
          rx_frame.data.u8[4] = *item & 0xFF;;
          rx_frame.data.u8[5] = (*item >> 8) & 0xFF;
          rx_frame.data.u8[6] = (*item >> 16) & 0xFF;
          rx_frame.data.u8[7] = (*item >> 24) & 0xFF;
          ESP32Can.CANWriteFrame(&rx_frame); // send items over CAN buss
          PassTwo = false;
        }

AntonioGR:
Not really. The real data I get is 0013a20040xxxxxx. A total of 16 bits. The first part is 0013a200 and the second one 40xxxxxx. That two parts are which I want to save in a byte each one. It may not be possible to do, but I dont really know. That's what I'm looking for, if you know how to do it or another better way.
Thank you.

It looks like you want to read the ASCII hex equivalent of an XBee address which is 64 bits, not 16!

You need to convert each digit to a 4-bit number and store 4 bits at a time. I can help later when I have more time.

Idahowalker:
Here you can see how I break down a 32 bit int,*item, into 4 bytes:

 if (!PassTwo)

{
          rx_frame.FIR.B.FF = CAN_frame_std;
          rx_frame.MsgID = 1;
          rx_frame.FIR.B.DLC = 8;
          rx_frame.data.u8[0] = *item & 0xFF;
          rx_frame.data.u8[1] = (*item >> 8) & 0xFF;
          rx_frame.data.u8[2] = (*item >> 16) & 0xFF;
          rx_frame.data.u8[3] = (*item >> 24) & 0xFF;
          PassTwo = true;
        } else {
          rx_frame.data.u8[4] = *item & 0xFF;;
          rx_frame.data.u8[5] = (*item >> 8) & 0xFF;
          rx_frame.data.u8[6] = (*item >> 16) & 0xFF;
          rx_frame.data.u8[7] = (*item >> 24) & 0xFF;
          ESP32Can.CANWriteFrame(&rx_frame); // send items over CAN buss
          PassTwo = false;
        }

Thank you Idahowalker. I appreciate your suggestion, I'll check it.

ToddL1962:
It looks like you want to read the ASCII hex equivalent of an XBee address which is 64 bits, not 16!

Yeah. I think that's what I need. I receive the address 0013a20040xxxxxx and I would like to store the LowNumber in a byte, and the HighNumber of the address in another byte. In case that is possible.
I would appreciate your next suggestion. Thank you mate.

Here is a sketch that converts a 16 character ASCII hex string to a 64-bit unsigned integer. It DOES NOT do any error checking on the content or length of the string.

uint64_t xbee_addr64;
char xbee_addrASCII[17] = "abcd56788765ABCD";

void setup()
{
  Serial.begin(9600);
  xbee_addr64 = stringTo64(xbee_addrASCII);
  Serial.print((unsigned long) (xbee_addr64 >> 32), HEX);
  Serial.println((unsigned long) xbee_addr64, HEX);
}

void loop()
{
}

uint64_t stringTo64 (char* str)
{
  uint64_t num64 = 0;
  for (int i = 0; i < 16; i++)
  {
    byte nibble;
    if (isdigit(str[i])) nibble = str[i] - '0';
    else nibble = toupper(str[i]) - 'A' + 0x0a;
    num64 = (num64 << 4) + nibble;
  }
  return num64;
}

AntonioGR:
Yeah. I think that's what I need. I receive the address 0013a20040xxxxxx and I would like to store the LowNumber in a byte, and the HighNumber of the address in another byte. In case that is possible.
I would appreciate your next suggestion. Thank you mate.

That address is 64 bits! It takes 8 bytes to store it in binary and 16 bytes to store it in ASCII. You cannot store it in 2 bytes!

ToddL1962:
That address is 64 bits! It takes 8 bytes to store it in binary and 16 bytes to store it in ASCII. You cannot store it in 2 bytes!

Oh Ok, know I know. Thank you very much. I saw your last post and I'll work on that. I hope I know how...

AntonioGR:
Oh Ok, know I know. Thank you very much. I saw your last post and I’ll work on that. I hope I know how…

If you need to store the address into an array rather than an unsigned 64-bit number I can help you with that.

AntonioGR:
Oh Ok, know I know. Thank you very much. I saw your last post and I'll work on that. I hope I know how...

I had a slight bug in the sketch I posted. I edited it so you need to get the latest.

ToddL1962:
I had a slight bug in the sketch I posted. I edited it so you need to get the latest.

Thanks for your time ToddL. Very kind. I will check and study it. Any question I will ask you.

ToddL1962:
If you need to store the address into an array rather than an unsigned 64-bit number I can help you with that.

Hi Todd.
I've been working on the sketch you sent me last week. But it doesn't seem to work for me. Maybe I do something wrong.

What I'm really trying to do is to unicast in a mesh network using different XBees S2. One as Coordinator and the rest as Routers. I wanted to store the address of every Router found in order to send data to them. For example: i would like to send 'Hi' to Router1, and then 'Bye' to Router2. Imagine I got 10 Routers. The idea is to send data whenever I want, to the specific Router.
I was wondering if this is possible. Do you have any advice?

Regards.