CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Bitwise Hex

  1. #1
    Join Date
    Oct 2011
    Posts
    5

    Unhappy 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. #2
    Join Date
    Oct 2011
    Posts
    5

    Re: Bitwise Hex

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

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    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.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  5. #5
    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. #6
    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. #7
    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 = %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. #8
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Bitwise Hex

    Quote Originally Posted by papriko View Post
    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.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

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

Featured