I consider myself still as a beginner, but I'm progressing on C programming... as fast as a french guy can.
I'm back with a new challenge for the community. I collect some binary values into a string. Then, I need to convert this into an Hex value..... It's now few hours / nights that I'm surfing on the web to test different things, but nothing works.
Just to tell you the story. I received a signal from a RFID reader. This signal is a 64 bits. I need to play with this string of bit to mix it differently and then convert this into hex value.
Well, if one of you can help me a bit...
Well already tried Strtoul() but certrainly in a wrong way. Here is part of the code (without the modifications on the string)
if (bitCount > 0 && flagDone) {
unsigned char i;
Serial.print("Read ");
Serial.print(bitCount);
Serial.print(" bits. ");
if (bitCount == 64)
{
for (i=0; i<64; i++)
{
cardCode <<=1;
Cardcsn[i] |= databits[i];
CardcsnString += String(Cardcsn[i]);
Serial.print (Cardcsn[i]);
}
SendHex();
}
else {
// you can add other formats if you want!
Serial.println("Unable to decode.");
}
// cleanup and get ready for the next card
bitCount = 0;
cardCode = 0;
for (i=0; i<MAX_BITS; i++)
{
databits[i] = 0;
}
}
}
void SendHex()
{
// I really hope you can figure out what this function does
Serial.println("csn = ");
Serial.print(CardcsnString);
}
You're so right. Well, the main objective of this is to take the 8 octets of a 64 bits and put these in a reverse order.
Example : what is read when using a wiegand reader :
0101111001001111001100100000000111110001111111100010010111000000
If I split into octet
00101111 00100111 10011001 00000000 11111000 11111111 00010010 11100000
And what I need to have
11100000 00010010 11111111 11111000 00000000 10011001 00100111 00101111
This last chains is to be converted into Hex.
I don't know anything about bitwise operator as of today, but I will read carefully your link and see if I can find a solution for this issue I got.
Try something like this.
(Warning: I did not test this code!)
unsigned long long x;
unsigned long long y;
// this reverses bytes of x and puts the result in y
// warning: this will destroy (zero out) x in the process
y = 0ULL;
for (byte i = 1; i <= 8; i++) {
y <<= 8; // make room for the next eight bits
y |= (x & 255ULL); // copy the last eight bits from x to y
x >>= 8; // drop the last eight bits from x
}
So then, I receive the data in a long, want to reverse it as explained, convert it in an hex value. This will be exported in an Ascii chain.
This last chain will be sent through an http request to a server as a variable.
I know how to do the last part., But wokring on type conversion is something new for me.
Lot of things for an end of week (a chance that this monday is a day off in France)
RGeeze:
So then, I receive the data in a long, want to reverse it as explained, convert it in an hex value. This will be exported in an Ascii chain.
I assume 'chain' means 'string'.
I may have misunderstood what you mean by 'reverse', but here's an example showing how you could convert the long value 0x12345678 to a hex string "0x78563412".