What is the difference?

Why output on Serial Monitor is different on this

#include <IRremote.h>

IRrecv irrecv(9);
decode_results results;

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

void loop() {
    if(irrecv.decode(&results)) {
        Serial.println(results.value, HEX);
        irrecv.resume();
    }
}

than on this

#include <IRremote.h>

IRrecv irrecv(9);
decode_results results;
String rem = "";

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

void loop() {
    if(irrecv.decode(&results)) {
        rem = (results.value, HEX);
        irrecv.resume();
    }
    Serial.println(rem);
}

I don't change anything on pins but when I use first code the output is how it should be but on second output is always "16" and nothing can change it. Does anybody knows why is this happening?

Edit1: By the way i want to save things like "FFA25D" or "FF10EF" in 'rem'.

rem = (results.value, HEX); What does the comma operator do?

Most likely HEX has been defined as 16, and you are setting rem to that. I'm suprised the compiler isn't giving a warning, that isn't proper format for an = statement.

The statement, while both unusual and not what the OP had in mind, probably, is valid C or C++ syntax.

Hint: read reply 1.

a7

The HEX in your Serial.print statement is an argument for Serial.print and tells it that you want to display the variable (first argument) in hexadecimal text representation. It's not a way in C/C++ to force a hexadecimal text representation if you don't use that function (or other print functions).

For your example, you can use the below

char rem[9];
sprintf(rem,"%02X", results.value);
Serial.println(rem);

This assumes that results.value is a max of 4 bytes (a 32 bit variable). The char rem[9] has place for the hexadecimal text representation of 4 bytes (hexadecimal text representation takes two characters per byte) plus the terminating NUL character. The sprintf function fills the rem array with the hexadecimal text representation of results.value.

Notes:
1)
sprintf is a little expensive on memory; that will only be an issue if you're running low on memory.
2)
It's advisable to learn not to use String (capital S) if you have limited RAM resources. If your code grows and you make heavy use of String concatenations, your code can start showing odd behaviour due to memory fragmentation.
3)
To limit what is placed in the character array, you can use snprintf; see the man page for sprintf/snprintf

I think @sterretje wanted to write

sprintf(rem, "%08X", results.value);

:wink:

Consider the OP's name together with the fact that this was his/her first Post

...R

AWOL:

rem = (results.value, HEX);

What does the comma operator do?

I thought this will save in rem this what would be printed on serial monitor in 1st code.

troll5662:
I thought this will save in rem this what would be printed on serial monitor in 1st code.

It doesn't because it is not calling the print() function.

Look up the sprintf() function if you want to get a text representation of a number

...R

Commas have several different meanings in C, they separate items in lists of arguments for example.

But in some contexts a comma is an operator that discards the LHS expression after evaluating it, and
returns the RHS result.

Its almost never useful and you've tripped over it.

alto777:
The statement, while both unusual and not what the OP had in mind, probably, is valid C or C++ syntax.

Hint: read reply 1.

a7

Anything beyond the "default" level of compiler warnings will show you the problem.

/home/pi/Downloads/arduino-1.8.8/hardware/arduino/avr/cores/arduino/Print.h:30:13: warning: left operand of comma operator has no effect [-Wunused-value]
 #define HEX 16
             ^
/home/pi/Arduino/sketch_may03b/sketch_may03b.ino:12:25: note: in expansion of macro 'HEX'
         rem = (results, HEX);
                         ^

Right, a warning. Valid C or C++.