How to convert a string to long int? [closed]
How to convert a string to long int? [closed]
I'm new to Arduino and my first project is an RFID reader. since I need a lot of known tags list , i have a problem with not enough memory. for that reason I want to convert strings like : "426d9244", "1265dd39"... to a long int. I know it's been asked but as a new programer I find it hard to understand. an example would be very appreciated also.
This question appears to be off-topic. The users who voted to close gave this specific reason:
2 Answers
2
If all those are known tags, you can put them in the source code as
32 bit integers. No
need to convert. Or rather, let the compiler do the conversion:
const uint32_t ID_num[ARRAYSIZE] PROGMEM =
Oxbcc0f1c3, Ox821a7d39, Ox4924887c, ...
;
When you read a tag number in hex, in order to compare it with the known
tags, you convert it to the same type using strtoul() from
avr-libc:
strtoul()
uint32_t tag_id = strtoul(tag_string, NULL, 10);
Actualy , it did the compare without even converting the tag_id... , it works perfect now, HOGE thanks!!!
– rafi shoval
Aug 26 at 10:14
Please "accept" the answer to show that it helped you. Thanks!
– Nick Gammon♦
Aug 26 at 23:23
It's a bit unclear what you want to do with the 'd'or 'dd'in between.
You can check some functions:
strtok: which can split a string based on delimiters (in your case probably 'd'). Than you get an array of strings (e.g. for the first: "426", "9244".
For each string ("426" and "9244") you use function atol to convert it into a long integer.
You could use the atol() function. However, I think that is not your problem. Perhaps you have blocks of code that is repeated a few times. Do you use the Serial.println in combination with the F() macro?
– Jot
Aug 26 at 9:37