int A=(mfrc522.uid.uidByte[0]);
int B=(mfrc522.uid.uidByte[1]);
int C=(mfrc522.uid.uidByte[2]);
int D=(mfrc522.uid.uidByte[3]);
Serial.println("UID is %x %x %x %x", A, B, C, D);
This results in the following output:
UID is 35 C5 5A 59
My issue is - how do I first reverse that UID : from 35 C5 5A 59 to 59 5A C5 35
and second convert and print the corresponding decimal value of that reversed UID. In this case, it would be 1499120949.
EDIT: I should mention that I need it as a string at the end to add it to the end of a POST command.
1. Reversing is relatively easy; you save your data in an array in the reverse order.
2. If you want to show the corresponding decimal value on the Serial Monitor/LCD Monitor, you can do it easily using print() method.
3. If you want to save the decimal digits of the corresponding decimal value, you need to do some math using %(modulus) and /(division) operators. Before doing that you have to bind the array elements into a single integer using a suitable data structure.
GolamMostafa: 1. Reversing is relatively easy; you save your data in an array in the reverse order.
2. If you want to show the corresponding decimal value on the Serial Monitor/LCD Monitor, you can do it easily using print() method.
3. If you want to save the decimal digits of the corresponding decimal value, you need to do some math using %(modulus) and /(division) operators. Before doing that you have to bind the array elements into a single integer using a suitable data structure.
Delta_G:
Or use itoa. That would be a lot simpler don't you think?
Print the data in reverse order like @AWOL said. Then when you get the data just use itoa.
Forget thinking that it is HEX or Decimal, it's always binary to the computer. Hex, dec, oct, it's all stored as the same binary. When you use itoa you get to specify what base you want the ascii to represent. The default will be decimal.
How can I go for from printing the data to having the data in a data structure that itoa will accept? I mean if I use the solution AWOL had - I would have just printed out the individual reversed hex's.
In order to use itoa I need to have those individual reversed hex's combined together and then put into itoa as an integer and it will spit out a decimal string.
int A=(mfrc522.uid.uidByte[0]);
int B=(mfrc522.uid.uidByte[1]);
int C=(mfrc522.uid.uidByte[2]);
int D=(mfrc522.uid.uidByte[3]);
char* final;
sprintf(final, "%x%x%x%x", D, C, B, A);
// int A=(mfrc522.uid.uidByte[0]);
// int B=(mfrc522.uid.uidByte[1]);
// int C=(mfrc522.uid.uidByte[2]);
// int D=(mfrc522.uid.uidByte[3]);
byte A = '0x35';
byte B = '0xC5';
byte C = 'Ox5A';
byte D = '0x59';
char final[sizeof(byte)*4];
sprintf(final, "%x%x%x%x", D, C, B, A);
Serial.print("%s\n", final);
Okay, I replaced the mfrc522 library with fixed hex codes. When I try and do the Serial.print - I'm getting errors
no known conversion for argument 2 from 'char [4]' to 'int'
exit status 1
call of overloaded 'print(const char [4], char [4])' is ambiguous
//
byte A = 0x35;
byte B = 0xC5;
byte C = 0x5A;
byte D = 0x59;
long revAndComb = ((unsigned long) D << 24) | ((unsigned long) C << 16) | ((unsigned long) B << 8) | A;
Serial.print("%ld\n", revAndComb);
Does that MATH line convert to decimal too? Also I'm getting an error on print line
no known conversion for argument 1 from 'const char [5]' to 'long unsigned int'
exit status 1
call of overloaded 'print(const char [5], long int&)' is ambiguous