VirtualWire, RF Link 315mhz, INT FROM CHAR

Hello,

I could use some help with what I think is a simple problem.
I have two Arduino UNOs, A 315mhz Receiver, and Transmitter (RF LINK) , And some simple code from here.

THE QUESTION: How can I send an integer (ie. 12), and receive an integer.
I don’t know how to convert the char to an int. I have tried many different ways on this forum, and cannot seem to get it to work. The receiving arduino prints a HEX value to the Serial Monitor like 35 for the number “5”.

I am sure there is something I am missing with the way I am sending the “char *controller;” variable, and receiving it. Thanks

Here is the code I am using below. One set of code for each arduino (Transmitter, Receiver).

Transmitter Arduino:

#include <VirtualWire.h>
char *controller;
void setup() {
vw_set_ptt_inverted(true); //
vw_set_tx_pin(12);
vw_setup(2000);// speed of data transfer Kbps
}

void loop(){
controller="5";
vw_send((uint8_t *)controller, strlen(controller));
vw_wait_tx(); // Wait until the whole message is gone

}

Receiver Arduino:

#include <VirtualWire.h> //Includes library VirtualWire.h
void setup() {
  
//++++++++++++++Initializes the receiver module+++++++++++++++++++
vw_set_ptt_inverted(true);
vw_setup(2000); 
vw_set_rx_pin(2); //Configure the pin D2 to read the data
vw_rx_start(); //Starts reading data from the receiver module
//==============================================================
Serial.begin(9600);
}

void loop() {
  
uint8_t buf[VW_MAX_MESSAGE_LEN]; //Variable for storing the data buffer   
uint8_t buflen = VW_MAX_MESSAGE_LEN; //Variable for storing the buffer size   
   if (vw_get_message(buf, &buflen)) // Non-blocking
    {
	int i;
	for (i = 0; i < buflen; i++)
	{
	    Serial.print(buf[i], HEX);             
	}
	Serial.println();
    }
}

The receiving arduino prints a HEX value to the Serial Monitor like 35 for the number "5".

Why does it do that? What earthly reason is there for printing the characters in hex?

Cast the byte array as a char array and print characters.

If you NULL terminate the string you get, you can use atoi() to convert the string to an int.

Hello, How would I "Cast the byte array as a char"? Then as an integer, or cast the byte array as an integer?

I want the result as an Integer. So.

Where the value comes in as "buf*" (See the second Code Block above), I want to convert that into a usable integer.* I tried: int myValue = int(buf[0]); This works, but does not give me an integer that is usable. For instance. If (myValue == 5) { * Serial.println("Found Value 5");* } else { * Serial.println("No Value Found");* } When I send the value 5, it does not work. I get the else statement. * *Thanks, captclearleft

How would I "Cast the byte array as a char"?

You don't You cast things that are the same SIZE. An array is NOT the same size as a char.

byte stuff[] = "This array is a byte array";

vw_send(stuff, strlen((char *)stuff)); // Cast the byte array to a char array because that's what strlen wants.
   if (vw_get_message(buf, &buflen)) // Non-blocking
   {
       buf[buflen] = '\0';
       Serial.print((char *)buf); // Cast the byte array to a char array

More stuff...

char *str = "123";

int valFromStr = atoi(str);

byte stuff[] = { '1', '2', '3', '\0' };

int valFromStuff = atoi((char *)stuff);