# Bitwise Hex

• October 5th, 2011, 11:25 AM
papriko
Bitwise Hex
Hello
I'm newbie here
I'm developing a GUI to manipulate the RS232 with a MyFARE Card Reader (some kind of RFID reader)
I order to write or to read a block from the card, I send Hex message like "16 00 49 ab 47 xx" where xx is the checksum (the bitwise in Hex) of all other numbers (so : 16, 00, 49, 47)
I Google it but all I can find is bitwise in binary not in hex, someone suggested that I convert all my numbers from hex to bin then do the bitwise with the '|' operator then convert back to hex .... I tried but actually never succeed it.
So is there anyway to manage this problem for me ?

I resume :
Input : 16 00 49 ab 47
output : the checksum of input
16 = 00010110
49 = 01001001
47 = 01000111
======================== (XOR columns)
x = 00011000 = 18

PS: i'm using Visual 2010
• October 5th, 2011, 11:29 AM
papriko
Re: Bitwise Hex
4th line I mean "In order to ....
ps : how do I edit my post ?
• October 5th, 2011, 02:33 PM
S_M_A
Re: Bitwise Hex
There should be an edit button in the right bottom corner of your posts.

Is this a school-assignment?

The example you showed is wrong. The result is 0x18 not 0x14.
The xor operator is ^ not | (or operator).
• October 5th, 2011, 03:05 PM
krmed
Re: Bitwise Hex
In reality, the correct result (checksum) is B3.

0001 0110 (16)
0000 0000 (00)
0100 1001 (49)
1010 1011 (ab)
0100 0111 (47)
---------
1011 0011

You must XOR all of the values: 16 00 49 ab 47 (yes, ab is a value that needs to be included).

Good luck.
• October 5th, 2011, 04:17 PM
papriko
Re: Bitwise Hex
S_M_A
No this is not a school assignment and yes I was wrong it's 18 not 14

krmed
I know what XOR is , I need it to work with Hex !
• October 5th, 2011, 04:19 PM
papriko
Re: Bitwise Hex
yeah another error pfff stupid I am
i miss the term ab
anyway I need the bitwise (XOR) to work with hex not binarys
• October 6th, 2011, 01:00 AM
ProgramArtist
Re: Bitwise Hex
Can you explain what you mean with "binary, not in hex"?

Hex is a numeral system, it represents values. Anyway all the values in your computer are still binaries (they consist of zero's and one's).

Code:

```__int32 a = 18; printf("dec = &#37;d  hex = 0x%0x\n", a, a);```
shoud print:
12 0x0c

Both numerals represent the same value which is:
0000 0000 0001 0010

The bitwise(sic!) xor operator in c/c++ is ^.

The name of the RFID card is Mifare.
• October 6th, 2011, 05:58 AM
krmed
Re: Bitwise Hex
Quote:

Originally Posted by papriko
S_M_A
No this is not a school assignment and yes I was wrong it's 18 not 14

krmed
I know what XOR is , I need it to work with Hex !

Acutally you are not correct here - the checksum is not 18 as you say - it should be B3 as mentioned in my earlier post.

The data you show is quoted, so I'm assuming you are wanting to send a string of characters (i.e. "16 00 49 ab 47") to your device, but you need the checksum. This being the case, you need to get your string into variables so you can use the xor on the. You might want to have something like
Code:

```unsigned char first = 0; unsigned char second = 0; unsigned char third = 0; unsigned char fourth = 0; unsigned char fifth = 0; unsigned char checksum = 0;```
Now you need to convert your string to put the values in the variable - perhaps sscanf can be your friend.

Now you can calculate your checksum:
Code:

`checksum = first ^ second ^ third ^ fourth ^ fifth;`
Hex is simply a way to display the data - it's still binary data just as shown before.

Hope that helps.
• October 6th, 2011, 06:50 AM
papriko
Re: Bitwise Hex
thank you all guys , i'm gonna try what you said krmed in the last post
if something goes wrong i'll be back :D , I know i'm being annoying sorry !!