Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Simple question regarding splitting up a Hex number into a buffer (http://www.go4expert.com/forums/simple-question-regarding-splitting-hex-t18389/)

pamela1985 6Jul2009 22:40

Simple question regarding splitting up a Hex number into a buffer
 
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 6Jul2009 23:16

Re: Simple question regarding splitting up a Hex number into a buffer
 
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 7Jul2009 00:03

Re: Simple question regarding splitting up a Hex number into a buffer
 
Thank you so much! :happy:
/pamela

SaswatPadhi 7Jul2009 05:34

Re: Simple question regarding splitting up a Hex number into a buffer
 
My pleasure :)

indiansword 7Jul2009 05:47

Re: Simple question regarding splitting up a Hex number into a buffer
 
pamela :) put this smiley in yahoo IM to see what it is " :X :-*" :)


All times are GMT +5.5. The time now is 11:58.