Go Down

Topic: long unsigned int (Read 2397 times) previous topic - next topic

Carlcox89

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); ?

larryd

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

No technical PMs.
The last thing you did is where you should start looking.

Carlcox89

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 ? :/ it's a bit confusing

larryd

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   http://www.pjrc.com/teensy/td_libs_IRremote.html
No technical PMs.
The last thing you did is where you should start looking.

Carlcox89

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 * :/

larryd

No technical PMs.
The last thing you did is where you should start looking.

Carlcox89

Thanks anyway, may be someone else can help ;)

majenko

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:
Code: [Select]

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:
Code: [Select]

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)

PaulS

There is a strtol() function to convert a string to a long.
The art of getting good answers lies in asking good questions.

majenko


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

Meh - that's taking the easy way out ;)

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

Code: [Select]

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

Quote

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?

holmes4

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

Mark

AWOL

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

sp. '\0'
"Pete, it's a fool (who) looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

majenko


Quote
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
Code: [Select]

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.

Carlcox89

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.

Go Up