Irsend : convert hex to long int

Hello, I use irsend library to send commands with IR transmitter. This code is working : Irsend.sendNec(0x68A90,20) I would like to send the command through ethernet. So the code 0x68A90 is stored into a string.

String cde ; cde = « 0x68A90 » ; Irsend.sendNec(cde,20) ;

This code fires an error message : error: no matching function for call to 'IRsend::sendNEC(String&, int)' note: candidates are: void IRsend::sendNEC(long unsigned int, int)

To convert a string to a long unsigned int I have tested this : Int iri = strtol (cde,0,16);

But this code generates the error message : JIDE02:139: error: cannot convert 'String' to 'const char*' for argument '1' to 'long int strtol(const char*, char**, int)'

So my question is : in this case (Irsend) how to convert a hex string to a long unsigned int ?

Thanks for your help.

The problem is the stupid String class. Ditch it. Learn to use c style strings, null terminated char arrays. It will save you tons of headache in the future.

dakota99:
JIDE02:139: error: cannot convert ‘String’ to ‘const char*’ for argument ‘1’ to ‘long int strtol(const char*, char**, int)’

Stop using String and use a char array instead.

Hello,

Thanks for your comments I'm not familiar with C++ ... I have tested the following code without success :

char irc[11] = "0x80BF51AE"; long int iri = strtol(irc,0,16); irsend.sendNEC(iri,32); Serial.print(iri);Serial.println();

There is no error message but the receiver does not receive data. But this code is correct : irsend.sendNEC(0x80BF51AE,32);

Does anyone understands what’s wrong ?

Thanks for your time

dakota99: char irc[11] = "0x80BF51AE"; long int iri = strtol(irc,0,16); irsend.sendNEC(iri,32); Serial.print(iri);Serial.println();

There is no error message but the receiver does not receive data.

Why define it as a string in the first place.

What's wrong with

unsigned long int iri = 0x80BF51AE;
irsend.sendNEC(iri,32);

Hello Thanks for your reply.

I define it as a string in the first place because the code is received by the arduino through a http request. (webserver)

So I have a string that I must convert into a long int.

Thanks.

dakota99: Hello Thanks for your reply.

I define it as a string in the first place because the code is received by the arduino through a http request. (webserver)

So I have a string that I must convert into a long int.

Thanks.

Well at the absolute minimum you'll need to define your irl as "unsigned long" not just "long"

Hello, Thanks again for your comment.

The code below is working : the receiver receives the correct data.

unsigned long int iri = 0x80BFC936; irsend.sendNEC(iri,32);

The problem seems the conversion from char to unsigned long int.

This code is not working : unsigned long int iri = atol("0x80BFC936"); irsend.sendNEC(iri,32);

What is the right way to convert a char to an unsigned long int ?

Thanks for your help

How does this work for you

unsigned long int iri = strtoul("80BFC936",0,16);

Fantastic, it works ! :D

char irc[11] = "0x80BFC936"; unsigned long int iri = strtoul(irc,0,16);

But this code does generates a message : String irs = "0x80BFC936"; unsigned long int iri = strtoul(irs,0,16);

As you have written, I must use a char. This code below works :

char irc[11]; String irs = readString.substring(8,18); // readString = command received from ethernet irs.toCharArray(irc, 11); unsigned long int iri = strtoul(irc,0,16); irsend.sendNEC(iri,32);

Thanks again for your help :)