Help with Variables

I'am currently trying to program a dev board based on ESP8266, which should be irrelevent to my question as I have all the required libraries working. The idea is that the baord connects to wifi, it then connects to an MQTT broker and subscribes to a topic, the topic payload is stored to a variable then the irsend command sends the payload as an 8 digit hex precedded by 0x. The problem I am having is storing the payload to a proper variable to be used in

irsend.sendNEC(Command_code, 32); // Send LGTV 1

with the variable type for Command_code being an unsigned int.
I am able to print the payload to the serial console using

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }

But whenever I try to manipulate the contents of payload I get weird responses. Please see the full code below for my current (not working attempt). The wifi connection and the MQTT connection and monitoring all working just fine. Please help and I am quite new to programming so keep it simple if you can.

#include <IRremoteESP8266.h>
#include <IRremoteInt.h>

#include <PubSubClient.h>

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <stdlib.h>     /* strtoul */ 
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager
const char* mqtt_server = "10.10.10.110";
unsigned int Command_code;
unsigned char code;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
IRsend irsend(4);
 
void setup_wifi() {
    // put your setup code here, to run once:
    Serial.begin(115200);
 
    //WiFiManager
    //Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wifiManager;
    //reset saved settings
    //wifiManager.resetSettings();
 
    //set custom ip for portal
    wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
 
    //fetches ssid and pass from eeprom and tries to connect
    //if it does not connect it starts an access point with the specified name
    //here  "AutoConnectAP"
    //and goes into a blocking loop awaiting configuration
    wifiManager.autoConnect("LinkNodeAP");
    //or use this for auto generated name ESP + ChipID
    //wifiManager.autoConnect();
 
    //if you get here you have connected to the WiFi
    Serial.println("connected... :)");
}
 
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
    unsigned char code = (code & (char)payload[i]) ;
  }
    unsigned int Command_code = (strtoul((code, NULL, 16));


  Serial.println();

  // Send the code to the IR emitter
  if (payload != 0) { 
     irsend.sendNEC(Command_code, 32); // Send LGTV 1
     Serial.print("Sending code ");
     Serial.print(Command_code, HEX);
  }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str(), "homeassistant", "benjamin")) {
      Serial.println("connected");
      client.subscribe("TV/command");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

}
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
    unsigned char code = (code & (char)payload[i]) ;
  }
    unsigned int Command_code = (strtoul((code, NULL, 16));

Note: These two uses of the variable 'code' refer to different variables. The first one is declared inside brackets so it is local to that set of brackets. The second one must be referring to an instance of 'code' declared somewhere else.

Still really need some help with this one. I am getting nowhere fast.

Did you understand reply #1 ?

Have you fixed the issue in the first response? Post the new code.

I did but that still does not help me accomplish what I need to.
this code works but does not return the expected value.

#include <IRremoteESP8266.h>
#include <IRremoteInt.h>

#include <PubSubClient.h>

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <stdlib.h>     /* strtoul */ 
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager
const char* mqtt_server = "10.10.10.110";
unsigned int Command_code;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
IRsend irsend(4);
 
void setup_wifi() {
    // put your setup code here, to run once:
    Serial.begin(115200);
 
    //WiFiManager
    //Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wifiManager;
    //reset saved settings
    //wifiManager.resetSettings();
 
    //set custom ip for portal
    wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));
 
    //fetches ssid and pass from eeprom and tries to connect
    //if it does not connect it starts an access point with the specified name
    //here  "AutoConnectAP"
    //and goes into a blocking loop awaiting configuration
    wifiManager.autoConnect("LinkNodeAP");
    //or use this for auto generated name ESP + ChipID
    //wifiManager.autoConnect();
 
    //if you get here you have connected to the WiFi
    Serial.println("connected... :)");
}
 
void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  } 
   Serial.println();
   unsigned int Command_code = (payload, HEX);
   Serial.print(Command_code);

    // irsend.sendNEC(Command_code, 32); // Send LGTV 1
Serial.println();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str(), "homeassistant", "benjamin")) {
      Serial.println("connected");
      client.subscribe("TV/command");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();

}
unsigned int Command_code = (payload, HEX);
   Serial.print(Command_code);

What are you trying to do with the "HEX" in there. It results in Command_code being set equal to HEX which IIRC is just 16 and it ignores payload. Maybe you wanted to print it in hex notation? In that case the HEX goes in the print line, not where you set the variable.

{too late}

I found some examples and I am able to store the HEX code in a string. But now when I go to convert that to an interger it looks like its being converted to decimal and modified.

void callback(char* topic, byte* payload, unsigned int length) 
{ 
 String Command_code; 
 {                                                     
 //put this in a sub block, so any unused memory can be freed as soon as possible, required to save mem while sending data
   char message_buff[length + 1];                  //copy over the payload so that we can add a /0 terminator, this can then be wrapped inside a string for easy manipulation.
   strncpy(message_buff, (char*)payload, length);   //copy over the data
   message_buff[length + 1] = '\0';  
   Command_code = String(message_buff);
   } 
Serial.print("Sending Code: ");
const char* number = Command_code.c_str();
int Hex_code = strtol((number), NULL, 16);
irsend.sendNEC(Hex_code, 32); // Send LGTV 1
Serial.print(Hex_code);
Serial.println();
}

For example when I send 0x20DF10EF my int returns 551538735

const char* number = Command_code.c_str();

You realize that you go through the big pain of building that representation into the temporary char array message_buff by having consideration of freeing the stack just above and then shoot holes into your Heap memory by using String object - and worse just after that going back to ask the char array representation to your String object??

message_buff[length + 1] = '\0';  that's a bug. You overflow. Will lead to disappointments.

Regarding your int, you are in a 32 bit architecture so int are 4 bytes but you probably need to think if you want to work with signed or unsigned representations or long int. The strtol() function returns a long not an int - I have not tested but that might be biting you.

ajplante:
For example when I send 0x20DF10EF my int returns 551538735

It's close. According to my programmer's calculator 0x20DF10EF is 551489775 so the first three digits are OK.
551538735 == 0x20DFD02F
Looks like two digits got modified: 1->D and E->2
When you print out 'Hex_code', how are you getting 32 bits out of a 16-bit int?!?
SHOW YOUR OUTPUT.

I actually have it spitting the exact decimal equivalent of the HEX value into a int now. I can even print it in HEX to the serial monitor. But I need it to be in HEX with the leading 0x when it is put into the irsend. How can I now format the number thusly.

void callback(char* topic, byte* payload, unsigned int length) 
{ 
  String Command_code; 
  {                                                     
  //put this in a sub block, so any unused memory can be freed as soon as possible, required to save mem while sending data
    char message_buff[length + 1];                  //copy over the payload so that we can add a /0 terminator, this can then be wrapped inside a string for easy manipulation.
    strncpy(message_buff, (char*)payload, length);   //copy over the data
    message_buff[length] = '\0';  
    Command_code = String(message_buff);
    } 
Serial.print("Sending Code: ");
const char* number = Command_code.c_str();
Serial.print(number);
Serial.println();
int Hex_code = strtol(number, NULL, 0);
irsend.sendNEC(Hex_code, 32); // Send LGTV 1
Serial.print(Hex_code, HEX);
Serial.println();
}

This is the output of the function

Sending Code: 0x20DFD02F
20DFD02F

Why all the messing about converting back and forth between strings and Strings ?

UKHeliBob:
Why all the messing about converting back and forth between strings and Strings ?

Ah, that is a good question. Because I am new to programming and I am stuck with a MQTT library that will only spit out a varialble in a byte array and an IRremote library that expects an unsigned int as an input. I am building a wifi universal remote for my living room.I want to keep as little device specific coding on the dev board as possible and instead send things like specific codes from my Raspberry Pi running home assistant. Which would facilitate adding and removing devices with realative ease. I pieced together this code largely from the examples in the library. I learn best by just jumping in. I have no prior C++ or related programming language experiance. Just HTML and PHP. My experiance with the arduino thus far has been just using ready made sketches and designing circuits.

ajplante:
This is the output of the function

Sending Code: 0x20DFD02F

20DFD02F

I still don't believe it is possible to print out 32 bits from a 16-bit 'int'.
The irsend.sendNEC(Hex_code, 32); is sending 32 binary bits. If you want to display them at the receiving end in hexadecimal with "0x" in front of them, that is something the receiver must do. If you want to display the hexadecimal number after sending it and you want to display it in hexadecimal with a leading "0x" then that is easy to do:

int Hex_code = strtol(number, NULL, 0);
irsend.sendNEC(Hex_code, 32); // Send LGTV 1
Serial.print("0x");
Serial.print(Hex_code, HEX);
Serial.println();

johnwasser:
I still don't believe it is possible to print out 32 bits from a 16-bit 'int'.

John - the OP said at the beginning

I'am currently trying to program a dev board based on ESP8266,

--> so int are 32 bits there unless I'm mistaken.