CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Bitwise Hex

1. Junior Member
Join Date
Oct 2011
Posts
5

## 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

thanks in advance
PS: i'm using Visual 2010
Last edited by papriko; October 5th, 2011 at 04:15 PM.

2. Junior Member
Join Date
Oct 2011
Posts
5

## Re: Bitwise Hex

4th line I mean "In order to ....
ps : how do I edit my post ?

3. Elite Member
Join Date
Oct 2006
Location
Sweden
Posts
3,651

## 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).
Last edited by S_M_A; October 5th, 2011 at 03:18 PM.

4. Senior Member
Join Date
Jan 2002
Location
Houston, TX
Posts
1,421

## 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.
Last edited by krmed; October 5th, 2011 at 03:08 PM.

5. Junior Member
Join Date
Oct 2011
Posts
5

## 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 !

6. Junior Member
Join Date
Oct 2011
Posts
5

## 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

7. Member
Join Date
May 2006
Location
Dresden, Germany
Posts
458

## 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.

8. Senior Member
Join Date
Jan 2002
Location
Houston, TX
Posts
1,421

## Re: Bitwise Hex

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.

9. Junior Member
Join Date
Oct 2011
Posts
5

## 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 , I know i'm being annoying sorry !!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)