Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hexInDec is really magic code #2

Open
Hcreak opened this issue Mar 21, 2022 · 1 comment
Open

hexInDec is really magic code #2

Hcreak opened this issue Mar 21, 2022 · 1 comment

Comments

@Hcreak
Copy link

Hcreak commented Mar 21, 2022

I'm trying to build my self FDX-B reader today. Thanks for the code for reference.

First, I want to file a bug. In the FDX-B protocol, TagNumber is 38 bits, i.e. 12 decimal digits.
Apparently unsigned long is not enough, long long should be used.
https://www.instructables.com/Animal-Micro-Chip-Scanner-RFID-Reader-Arduino/ This friend did fix your code.

In Part of convert Hex to Dec, I’m trying to Use strtoll function first. But as mentioned here https://forum.arduino.cc/t/strtol-stuck-with-big-numbers/697074/3

Which architecture are you using? 8-bit AVR or 32-bit processor? There is limited support for 64-bit numbers in the AVR implementation in the Arduino environment. It seems that e.g. strtoll() (note the double 'L') is not available; in which case you need to roll your own. And there is no print function to print 64-bit numbers as a any text representation.

Unless I switch to another microcontroller to use the strtoll function.
Your magic code led me to another solution.
Can you briefly explain the principle? Thank you very much.

@dun-n
Copy link

dun-n commented Apr 15, 2023

I spent some time figuring this out today in case you still want to know or any one else is confused.

hexInDec is taking the value sent by the module as a HEX string (a string of ascii characters that represent the value in HEX) and converting it to a decimal number. For example, let look at the 4 bytes that represent the country code. The module will send the ascii string 6D30. The module sends the characters backwards so really its 03D6 and if we convert that to decimal it should be 982. So basically this function is looking at the character codes of each char in the string "6D30" and using them to compute the decimal value.

It needs to find the decimal value for each hex digit in the string:
6 -> 6
D -> 13
3-> 3
0 - > 0

Then it needs to multiply each by the appropriate power of 16 and add them:
(61)+(1316)+(3*(16^2))+(0*(16^3)) = 982

If that's still confusing I'd look for a resources that explains converting between two number bases. maybe this?

as for printing the large number I converted it to a uint64_t and used the print function described here

hope that makes some sense or at least points someone in the right direction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants