beic
1
Hi there,
I'm trying to convert Hexadecimal to Decimal number, but I have some issues with my formula.
Sometimes of HEX (8 chars) I got DEC as (9 chars), but I always need to get 10 DEC chars.
Example input:
A041FD73 = 2688679283 (good)
22D9B634 = 584693300 (wrong)
Formula:
uint8_t uid[16] = {0};
unsigned long decRes = 0;
for (uint8_t i = 0; i < 4; i++) {
decRes <<= 8;
decRes += uid[i];
}
Serial.println(decRes);
Thank you,
Kind regards,
Viktor
yfuq
3
0x22D9B634 is indeed 584693300.
And 0x000000010 is 16, just 2 digits.
Your code is correct. You can't have 10 digits if your number is less than 10¹¹.
You can pad your number with zeros.
beic
4
yfuq:
0x22D9B634 is indeed 584693300.
And 0x000000010 is 16, just 2 digits.
Your code is correct. You can't have 10 digits if your number is less than 10¹¹.
You can pad your number with zeros.
Starting or ending zero to add?
I must to follow NFC RFID standard.
Thank you!
gcjr
5
looks like you just want a 10 digit string for display
results from following code
2688679283
0584693300
unsigned long a = 0xA041FD73;
unsigned long b = 0x22D9B634;
char s [40];
void setup (void) {
Serial.begin (115200);
sprintf (s, "%010lu", a);
Serial.println (s);
sprintf (s, "%010lu", b);
Serial.println (s);
}
void loop (void) {}
beic
6
gcjr:
sprintf (s, "%010lu", a);
Your example solved everything, thank you! 