Simple question regarding splitting up a Hex number into a buffer

pamela1985's Avatar, Join Date: Jul 2009
Newbie Member
Hello,
Im 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 Id like to display on the screen just 18. Which is the best way to do this?
Thanks you so very much!

/Pamela
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
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: C
sprintf(MyString, "%x", 0xFE18);
(2) Print the last two chars of the string :
Code: C
printf("%s\n", MyString+(strlen(MyString)-2));

Complete test program:
Code: C
#include <cstdio>
#include <cstring>

int main()
{
    char MyString[10];
    sprintf(MyString, "%x", 0xFE18);
    printf("%s\n", MyString+(strlen(MyString)-2));
    return 0;
}
pamela1985's Avatar, Join Date: Jul 2009
Newbie Member
Thank you so much!
/pamela
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
My pleasure
indiansword's Avatar, Join Date: Oct 2008
Security Expert
pamela put this smiley in yahoo IM to see what it is " :X :-*"