Hexadecimal after convert ends up different

First of all. I didn't write this code and I'm not really great at this.
I have a wristband with a RFID card inside that has the hexadecimal value 0dffaefa.
However after scanning on a card reader(which I think reads the RFID card as byte/decimal) it then proceeds to convert it to hexadecimal which end up like this FAAEFF0D.

The convert code looks like this

char bintohexascii(byte x)
{
	char hex[16] = {
		'0', '1', '2', '3', '4', '5', '6', '7',
		'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
	};
	return hex[x & 0x0f];
}

While the scanning code looks like this

void readCardF(){
  //Serial.println("Reading card");
  mySerial.write(readCard, sizeof(readCard));

  while(mySerial.available()&&readComplete<1){
    byte C = mySerial.read();
    cardBytesRead++;

      //if (C<16) Serial.print("0");
      //Serial.print(C,HEX);
      if ((cardBytesRead>4)&&(cardBytesRead<9)){
        //Serial.println("Card Bytes >4 and <9");
      byte hinibble = (C >> 4) & 0x0f;
      byte lonibble = C & 0x0f;
      tag[tagindex++] = bintohexascii(hinibble);
      tag[tagindex++] = bintohexascii(lonibble);
      } //End If
      //out_flag =1;

  if (cardBytesRead==9) {
          //Serial.println("CardBytesRead=9");
          tagindex=0;
          cardBytesRead=0;
          readComplete=1;
	   Serial.println("RFID tag value= ");
	   Serial.println(tag);
           strTagID=tag;
           //out_flag = 0;
	   isReplyRead=0;
	   if(strTagID.length()==8){
               verifyID();
            }
            else
            {
                Serial.println("Invalid Length of RFID Card");
                resetFunc();
            }
            return;
	}//End If carybytesread==9
  }//End While
} //End Function

It looks like the card reader is reading the values from the opposite way?
Is there a way to make the card reader read the hexadecimal the way I want it too, or is there maybe a way in C# to convert FAAEFF0D back to 0dffaefa?
I would prefer the C# way but both works fine.

Thanks in before hand

Is there a way to make the card reader read the hexadecimal the way I want it too, or is there maybe a way in C# to convert FAAEFF0D back to 0dffaefa?

The card reader reads the card one way. You can't change that. You can change the order of the data that it returns, if you have some strange need to.

0dffaefa is a rather non-standard way of expressing a HEX value. Typically, upper case letters are used. So, that would be 0DFFAEFA. Now, if you look at that as pairs, it is 0D, FF, AE, and FA.

If you look at FAAEFFOD the same way, you see FA, AE, FF, and 0D.

Its the same set of pairs, in reverse order.

Post all of your code, or simply compare the tag values as read against a table of known values arranged in the same order.

PaulS:

Is there a way to make the card reader read the hexadecimal the way I want it too, or is there maybe a way in C# to convert FAAEFF0D back to 0dffaefa?

The card reader reads the card one way. You can't change that. You can change the order of the data that it returns, if you have some strange need to.

0dffaefa is a rather non-standard way of expressing a HEX value. Typically, upper case letters are used. So, that would be 0DFFAEFA. Now, if you look at that as pairs, it is 0D, FF, AE, and FA.

If you look at FAAEFFOD the same way, you see FA, AE, FF, and 0D.

Its the same set of pairs, in reverse order.

Post all of your code, or simply compare the tag values as read against a table of known values arranged in the same order.

That's the important part of the code, the rest is just some random gibberish and a HTTP GET were we send the hexadecimal to a server.
The issue is that the server is expecting 0dffaefa not FAAEFFOD.
But I can look into converting FAAEFFOD back to 0dffaefa server-side

That's the important part of the code, the rest is just some random gibberish and a HTTP GET were we send the hexadecimal to a server.

The variable declarations are hardly random gibberish. But, it's your code and your problem.

The issue is that the server is expecting 0dffaefa not FAAEFFOD.

So, the server has some unrealistic expectations. Well, OK. I can't see any way to fix that.

But I can look into converting FAAEFFOD back to 0dffaefa server-side

Converting the string from all upper case to all lower case is a simple matter of changing the letters in the first snippet. Reordering the characters in the string (assuming that it IS a string; see comment above), would be trivial on the sending end.

PaulS:

The issue is that the server is expecting 0dffaefa not FAAEFFOD.

So, the server has some unrealistic expectations. Well, OK. I can't see any way to fix that.

How is it a unrealistic expectation? The RFID card hexadecimal value is 0dffaefa and the server is set to expect that.
But the Arduino reads it the other way around and thus returns the wrong hexadecimal

Google "endianness"

Then simply change your code to put the bytes into the array in the reverse order.

      tag[tagindex++] = bintohexascii(hinibble);
      tag[tagindex++] = bintohexascii(lonibble);
      tag[tagindex--] = bintohexascii(lonibble);
      tag[tagindex--] = bintohexascii(hinibble);

Of course you have to set tagindex to the last entry first.

AWOL:
Google "endianness"

Grumpy_Mike:
Then simply change your code to put the bytes into the array in the reverse order.

      tag[tagindex++] = bintohexascii(hinibble);

tag[tagindex++] = bintohexascii(lonibble);






tag[tagindex--] = bintohexascii(lonibble);
     tag[tagindex--] = bintohexascii(hinibble);



Of course you have to set tagindex to the last entry first.

Thanks. I'll check it out.

The RFID card hexadecimal value is 0dffaefa

Do you have something to back up this assertion? Clearly, the way that you are reading the value doesn't back up that assertion.

You read a value from a tag. You can accept that the value read IS the value of the tag, and put that number/string on the server.

Now, if there is something written on the tag (rendering it insecure) that you are trying to match to, then I'd expect that you'd have mentioned that by now.

If you want to convert an hexadecimal ascii string from upper case to lower case, just bitwise OR every character with 0x20 (doesn't affect numbers, but converts letters from upper to lower case).