VirtualWire converting result to int

Hi,

I’m using the VirtualWire library to send an int using the Itoa function. I’d like an int on the receiver side as well, but the library receives the data as an array of bytes, so I dont think Atoi would work.

I can’t find any simple way of just getting or converting to an int as a result, and it seems such a simple thing I know I’m missing something huge. Thanks.

One way to interconvert int and char[] data is to overlay them in a union. Something like this:

union {
  uint8_t bytes[2];
  int intval;
} my_data;

      :
      :

my_data.intval = 1234;
result = vw_send(my_data.bytes, sizeof(my_data));

Or you can simply cast a pointer to your integer to the proper type:

 int intval = 1234;

      :
      :

result = vw_send((uint8_t *)&intval, sizeof(intval));

I see. Perhaps I’m not understanding you, my issue is not sending an integer value on the transmitter side. Rather, it is getting something usable out of the receiver side. All the examples for the library just show it printing the result to serial. I don’t care about this, I need an integer–however, all I have is a byte[/i ]array. And the code won’t compile if I try to change the variable type.
This is essentially what I’m working with:
* *  uint8_t buf[VW_MAX_MESSAGE_LEN];   uint8_t buflen = VW_MAX_MESSAGE_LEN;     if (vw_get_message(buf, &buflen)) // check to see if anything has been received   {     // Message with a good checksum received.     for (int i = 0; i < buflen; i++)     {     Serial.print(buf[i]);   }* *
I’d like buf as an Int.

I decided to tackle this by converting the result into a char array and then using atoi.

char data[5];
int counter;

void loop {

 uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  
  if (vw_get_message(buf, &buflen)) // check to see if anything has been received
  {
   // Message with a good checksum received.

      buf[buflen] = '\0'; //Add a null terminator

      data[0] = buf[0];
      data[1] = buf[1];
      data[2] = buf[2];
      
      counter = atoi(data);

  }

  Serial.println(counter);
}