1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple question regarding splitting up a Hex number into a buffer

Discussion in 'C' started by pamela1985, Jul 6, 2009.

  1. pamela1985

    pamela1985 New Member

    Joined:
    Jul 6, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hello,
    I’m new to this forum and fairly new to programming.
    I have a hexadecimal number that I wish to put into a buffer so I can display the 2 least significant bits, that is the two last digits. i.e the number look like this unsigned short = 0xFE18 but I’d like to display on the screen just “18”. Which is the best way to do this?
    Thanks you so very much!

    /Pamela
     
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    Welcome to G4EF, Pamela :)

    Don't know, if this is the best, bu you can do it this way:

    (1) Print the hex number to a string:
    Code:
    sprintf(MyString, "%x", 0xFE18);
    (2) Print the last two chars of the string :
    Code:
    printf("%s\n", MyString+(strlen(MyString)-2));
    Complete test program:
    Code:
    #include <cstdio>
    #include <cstring>
    
    int main()
    {
        char MyString[10];
        sprintf(MyString, "%x", 0xFE18);
        printf("%s\n", MyString+(strlen(MyString)-2));
        return 0;
    }
     
  3. pamela1985

    pamela1985 New Member

    Joined:
    Jul 6, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Thank you so much! :happy:
    /pamela
     
  4. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,343
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
  5. indiansword

    indiansword Security Expert

    Joined:
    Oct 19, 2008
    Messages:
    496
    Likes Received:
    36
    Trophy Points:
    0
    Occupation:
    Operation Planner for 3 Australia
    Home Page:
    pamela :) put this smiley in yahoo IM to see what it is " :X :-*" :)
     

Share This Page