CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 1999
    Location
    Penang, Malaysia
    Posts
    65

    how to get the last 2 digits of an hex value

    Hi

    I hv one hex value
    0xFFFFFFAA in an array names Value[0]

    now I try to get the AA value only
    how can I do it

    can someone provide some clue

    Thanks
    sllai

  2. #2
    Join Date
    Jun 2002
    Posts
    1,417
    What data type is Value[0]? If it is char*, then:
    int len = strlen(Value[0]);
    char *last_two_digits = &value[0][len-2];

  3. #3
    Join Date
    Oct 1999
    Location
    Penang, Malaysia
    Posts
    65
    Thanks

    I try to combine two value

    value[0] = 0x000000AA
    value[1] = 0x000000BB

    to become a new value
    new = 0xAABB0000000000

    how can I implement it

    thanks in advanced

  4. #4
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    I hope you are doing this in C and not C++. new is a reserved word in C++.

    new = (value[0] << 24) | (value[1] << 16);
    Succinct is verbose for terse

  5. #5
    Join Date
    Jun 2002
    Location
    Germany
    Posts
    1,557
    You must mask and possibly shift your data in value[], as cup has suggested. Familiarize yourself with these types of operations.

    The answer to your direct question regarding the "AA" part of "FFFFFFAA" (written in plain C) is:

    unsigned long int dword_aa_part;

    <maybe other lines of code>...

    dword_aa_part = ((unsigned long int) value[0]) & 0x000000FF;

    This was an easy example, since merely a mask was needed.
    Some of your goals will involve masking and subsequently shifting by 8, 16, or 24 (again, see the example from cup). Typical masks will be 0xFF000000, 0x00FF0000, 0x0000FF00, and 0x000000FF. The right shifft operator >> and left shift operator << will be needed (see example from cup).

    Please watch out for the storage convention of the underlying compiler. The calculations will always work out, however the view of the raw data, for example in a memory watch-window, could look "byte-swapped" to the untrained eye. You will get it after some training. Be sure to use a good debugger.

    Good luck! Chris.

    You're gonna go blind staring into that box all day.

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