long unsigned int

I'm making some confusion with char* and long unsigned int and i cant seem to get what i want.
For example, if i have
char *command = "0xE0E058A7";

and i need to send that exactly 0xE0E058A7 as a long unsigned int (because IR library only accepts it as long unsigned int) how can it be done ?
so that in the end i get this: Irsend.sendSamsung(0xE0E058A7, 42); ?

Not sure I understand
unsigned long x=0xE0E058A7;
OR
use an array

Let me explain it better,
a "char *command" is sent to Arduino B from Arduino A (via nrf24 chip)
then i must use that char *command on Arduino B to send an IR signal using the function Irsend.sendSamsung(long unsigned data, int nbits);
As you can see the function doesnt accept char *, i need a way to get that char *command to be converted so the function accepts it and interprets it correctly.
For example if i send char *command = "0xE0E058A7";
i need that the function gets it exactly as it is but as a long unsigned, got it ? :confused: it's a bit confusing

char *command = "0xE0E058A7";
Serial.print(command); //prints 0xE0E058A7 on the Serial monitor

I have not worked with these so I am probably way off base, but have you tried?

char *command = "0xE0E058A7";
Irsend.sendSamsung(command, 42); ?

You obviously seen this IRremote Library, Send & Receive Infrared Remote Control

Yes mate, that was the first thing i tried, bu the problem is that the function must receive an unsigned long and not a char * :confused:

I tried :confused:

Thanks anyway, may be someone else can help :wink:

You need to parse the string and convert the hexadecimal characters into numbers to then add them into an unsigned long.

A simple function to convert a single hexadecimal character into its numeric representation is:

unsigned char h2d(unsigned char hex)
{
    if(hex > 0x39) hex -= 7; // adjust for hex letters upper or lower case
    return(hex & 0xf);
}

You can then use that with bitshifting to get your number:

char *command = "0xE0E058A7";
unsigned long value = 0;

// Start from the 3rd character
for (byte i = 2; i < 10; i++) {
  value <<= 4;  // Shift left to make room for 4 more bits (1 hex character)
  value |= h2d(command[i]);  // Add that new 4 bit into the number
}
// value now contains 0xE0E058A7 or 3772799143

(note: untested)

There is a strtol() function to convert a string to a long.

PaulS:
There is a strtol() function to convert a string to a long.

Meh - that's taking the easy way out :wink:

Be sure to read the man page on setting the base...

       long int strtol(const char *nptr, char **endptr, int base);

The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional '+' or '-' sign. If base is zero or 16, the string may then include a "0x" prefix, and the number will be read in base 16; otherwise, a zero base is taken as 10 (decimal) unless the next character is '0', in which case it is taken as 8 (octal).

The 0x at the start should force it to base 16 if you leave the base at 0, but to be safe I'd set it to 16 regardless.

However, all that extra code to convert other bases that you don't need would add extra bulk to your program - better to have a single small lightweight routine that just handles the one base you're working with, yes?

None of the above will work as your missing the "/0"!

Mark

None of the above will work as your missing the "/0"!

sp. '\0'

AWOL:

None of the above will work as your missing the "/0"!

sp. '\0'

"0xE0E058A7" is a string literal. As such there is an automatic implied "\0" at the end put in by the compiler. The simple fact that it's surrounded by double quotes turns it into "0xE0E058A7\0".

Now, if it were

char command[] = {'0', 'x', 'E', '0', 'E', '0', '5', '8', 'A', '7'};

then you'd be right, but it isn't, and you aren't.

I ended up using:

char *command = "0xE0E058A7";
unsigned long value = 0;
value = strtoul(command , 0, 16);

At first it wasnt working for me for some reason and it was making me confuse because
the irsend function had to receive this exact input '0xE0E058A7', and i thought that if i received that as a char* and then converted to unsigned long it would not be the same value as exatcly '0xE0E058A7'
thanks for all your help.